XQuery - 去掉标签但保留其文本

发布于 2024-10-19 07:22:24 字数 672 浏览 5 评论 0原文

如何在 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.

它缺少 其中的一些文本 部分。

关于如何返回这是我需要的一些文本。有什么想法吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唯憾梦倾城 2024-10-26 07:22:25

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

string(/root/childnode)

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)
全部不再 2024-10-26 07:22:25

使用

/*/childnode//text()

当在提供的 XML 文档上计算此 XQuery 时:

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

生成所需的正确结果:

This is some text that I need.

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.
歌入人心 2024-10-26 07:22:25

此 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文