Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between text() and string()

Tags:

xpath

Can someone explain the difference between text() and string() functions. I often use one with other, but it does not make any difference, both will get the string value of the xml node.

like image 364
Jayy Avatar asked Feb 29 '12 04:02

Jayy


People also ask

What is the difference between text and string in rails?

As a general rule of thumb, use :string for short text input (username, email, password, titles, etc.) and use :text for longer expected input such as descriptions, comment content, etc. For MySQL - not so much, you can have indexes on varchars, you cannot on text.

Is a text field a string?

A text input field's value is a string, so any validators must accept a string and return a string, null , or undefined .

What is string and text data?

A text string, also known as a string or simply as text, is a group of characters that are used as data in a spreadsheet program. Text strings are most often comprised of words, but may also include letters, numbers, special characters, the dash symbol, or the number sign.

What is a text string in Java?

The Java String data type can contain a sequence (string) of characters, like pearls on a string. Strings are how you work with text in Java. Once a Java String is created you can search inside it, create substrings from it, create new strings based on the first but with some parts replaced, plus many other things.


1 Answers

Can someone explain the difference between text() and string() functions.

I. text() isn't a function but a node test.

It is used to select all text-node children of the context node.

So, if the context node is an element named x, then text() selects all text-node children of x.

Other examples:

/a/b/c/text() 

selects all text-node children of any c element that is a child of any b element that is a child of the top element a.

II. The string() function

By definition string(exprSelectingASingleNode) returns the string value of the node.

The string value of an element is the concatenation of all of its text-node descendents -- in document order.

Therefore, if in the following XML document:

<a>   <b>2</b>   <c>3     <d>4</d>   </c>   5 </a> 

string(/a) returns (without the surrounding quotes):

"   2   3     4    5 " 

As we see, the string value reflects three white-space-only text-nodes, which we typically fail to notice and account for.

Some XML parsers have the option of stripping-off white-space-only text nodes. If the above document was parsed with the white-space-only text nodes stripped off, then the same function:

string(/a) 

now returns:

"23     4   5 " 
like image 88
Dimitre Novatchev Avatar answered Sep 28 '22 08:09

Dimitre Novatchev