kpozin
kpozin

Reputation: 26939

XQuery: Return value of an element rather the element itself

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?

Upvotes: 27

Views: 52665

Answers (5)

Aniruddha Jagtap
Aniruddha Jagtap

Reputation: 405

OSB (oracle service bus) users can use following function.

fn-bea:serialize($xml)

Upvotes: 0

savemaxim
savemaxim

Reputation: 377

Cast to xs:string {xs:string($xml/foo)}

Upvotes: 0

SeeJay
SeeJay

Reputation: 99

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

return data($xml)

Upvotes: 9

Oliver Hallam
Oliver Hallam

Reputation: 4262

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

return string($xml)

Upvotes: 20

Harold L
Harold L

Reputation: 5254

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()

Upvotes: 34

Related Questions