Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between data() and string()

What is the practical difference between the data() function and the string() function in Xquery? It seems they both return the same results when I use them. Can someone give me a simple example of how they are different?

let $dataset := 
  <data-set cool="rad">
    <!--- This is a comment -->
    <pages cool2="rad2" more="i guess">
      <page>
        <title>Puppy</title>
        <content>Puppies are great!</content>        
      </page>
      <page>
        <title>Dogs</title>
        <content>Dogs are grown up!</content>        
      </page>
      <page>
        <title>Puppy</title>
        <content>Puppies are great!</content>        
      </page>
    </pages>
  </data-set>

return $dataset/string()
(: data() returns the same thing as string() :)
(: return $dataset/data() :)
like image 859
aashtonk Avatar asked Jul 12 '26 10:07

aashtonk


1 Answers

string() will always return a string, whereas data() returns the "typed value". Most of the time this will be xs:string; however, it can also be xs:untypedAtomic or any other atomic type (typically defined in a schema).

There are some unintuitive cases too. For example, getting data() on a document node will return the string value of the node, but as an xs:untypedAtomic. As a general rule, if you want a string, use string() not data().

See the spec for all the details: http://www.w3.org/TR/xpath-functions/#func-data.

And this section has a very good explanation of the difference between typed- and string-data http://www.w3.org/TR/xpath-datamodel/#typed-string-relationships.

like image 158
wst Avatar answered Jul 16 '26 01:07

wst