4

如何在 XQuery 中去除一组标签,但仍将其文本保留在那里?例如,如果我有:

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

如何去除不需要的标签以获得:

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

实际上,我真正想要的只是文本,例如:

This is some text that I need.

当我执行以下操作时:

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

我得到:

This is  that I need.

它缺少它的some text一部分。

关于如何退货的任何想法This is some text that I need.

谢谢你。

4

3 回答 3

5

您感兴趣的不是子节点的字符串值(与文本节点序列或简化元素相反)吗?您可以从fn:string获取字符串值:

string(/root/childnode)
于 2011-02-26T21:18:51.747 回答
2

使用

/*/childnode//text()

在提供的 XML 文档上评估此 XQuery 时:

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

产生了想要的正确结果:

This is some text that I need.
于 2011-02-26T04:29:03.880 回答
0

这个 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(/*)

输出:

<root>
    <childnode>This is some text that I need.</childnode>
</root>
于 2011-02-26T00:46:21.287 回答