XQuery - Strip out tag but leave its text

3k Views Asked by At

How do I strip out a set of tags in XQuery but still leave its text there? For example, if I have:

<root>
    <childnode>This is <unwantedtag>some text</unwantedtag> that I need.</childnode>
</root>

How do I strip out the unwanted tag to get:

<root>
    <childnode>This is some text that I need.</childnode>
</root>

Actually, what I really want to end up with is just the text, like:

This is some text that I need.

When I do the following:

let $text := /root/childnode/text()

I get:

This is  that I need.

It's missing the some text part of it.

Any ideas on how to return This is some text that I need.?

Thank you.

3

There are 3 best solutions below

0
On BEST ANSWER

Isn't it the string-value of childnode, that you are interested in (as opposed to a sequence of text nodes, or a simplified element)? You can get the string-value from fn:string:

string(/root/childnode)
0
On

This XQuery:

declare function local:copy($element as element()) {
   element {node-name($element)}
           {$element/@*,
            for $child in $element/node()
            return if ($child instance of element())
                   then local:match($child)
                   else $child
           }
};
declare function local:match($element as element()) {
   if ($element/self::unwantedtag)
   then for $child in $element/node()
        return if ($child instance of element())
               then local:match($child)
               else $child
   else local:copy($element)
};
local:copy(/*)

Output:

<root>
    <childnode>This is some text that I need.</childnode>
</root>
0
On

Use:

/*/childnode//text()

When this XQuery is evaluated on the provided XML document:

<root>
 <childnode>This is <unwantedtag>some text</unwantedtag> that I need.</childnode>
</root>

the wanted, correct result is produced:

This is some text that I need.