ArcticDoom
ArcticDoom

Reputation: 84

Is it possible to get child nodes with the node name as text?

I have this weird one-off that I need to do with xpath and I don't even know if it's possible. So far, I haven't found a way.

<things>
<stuff>here are some things <yep>blue</yep> and here are some more things</stuff>
</things>

Basically, I want to be able to get the text of "stuff" and all its children as if they are text as well. For example I want to get this:

here are some things <yep>blue</yep> and here are some more things

Upvotes: 1

Views: 51

Answers (1)

zx485
zx485

Reputation: 29042

You can achieve this with the following XPath-2.0 expression:

for $p in /things/stuff/node() return if ($p instance of element()) then concat('&lt;',$p/name(),'&gt;',$p,'&lt;/',$p/name(),'&gt;') else $p

But this is not a recursive solution, so it works only for the first level of elements.

Upvotes: 4

Related Questions