tags:

views:

905

answers:

3

I have an XML document that contains the following

...
<foo>abc</foo>
...

If I evaluate

return $xml//foo

I get back

<foo>abc</foo>

Is there any way to get just abc instead?

+3  A: 

Yes, you want the text() function for selecting the child text:

return $xml/text()

Be careful if you will have nested tag structure inside $xml though, because this will only go one level deep for text nodes. If you want all of the text nodes together, stripping out other XML structure, this will do arbitrarily deep:

return $xml//text()
Harold L
Thanks! Just found that in the XQuery references.
kpozin
+2  A: 

Use the string function to get the string content of a node.

return string($xml)
Oliver Hallam
This is actually a more idiomatic and better alternative than the preferred answer above. It will also handle any nesting of child nodes correctly.
Pavel Minaev
A: 

To return only the data inside an element you can use:

return data($xml)
seejay