如何使用XPath表达式选择所有叶节点?

发布于 2024-09-27 04:26:11 字数 122 浏览 0 评论 0原文

我相信这是可能的,但无法弄清楚语法。像这样:

xmlNode.SelectNodes("//*[count(child::*) <= 1]")

但这不正确。

I believe it's possible but couldn't figure out the syntax. Something like this:

xmlNode.SelectNodes("//*[count(child::*) <= 1]")

but this is not correct.

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

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

发布评论

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

评论(4

橙幽之幻 2024-10-04 04:26:11

使用

//node()[not(node())]

如果只需要元素叶节点(这需要澄清——具有非元素子元素的元素是否被视为叶节点?),则使用以下 XPath 表达式选择它们:

//*[not(*)]

上面的两个表达式可能是选择所需节点(任意节点或元素 - 叶节点)的最短

Use:

//node()[not(node())]

In case only element leaf nodes are wanted (and this needs clarification -- are elements that have non-element children considered leaf nodes?), then the following XPath expression selects them:

//*[not(*)]

Both expressions above are probably the shortest that select the desired nodes (either any-node or element -- leaf nodes).

戏蝶舞 2024-10-04 04:26:11

任何没有子元素的元素

//*[not(child::*)]

Any elements with no element child

//*[not(child::*)]
寄与心 2024-10-04 04:26:11

为什么小于或等于 1 ?

xmlNode.SelectNodes("//*[count(child::*) = 0]")

在此站点进行测试等 http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

非常有帮助..

Why less or equal to 1 ?

xmlNode.SelectNodes("//*[count(child::*) = 0]")

Make tests etc at this site http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

Pretty helpful ..

颜漓半夏 2024-10-04 04:26:11

我添加这个 XSLT 答案是因为谷歌的前端匹配似乎缺乏这样的解决方案:

经过长期努力将 CDATA 提取为 XML,最终,这个表达式最适合我:

<xsl:template match="*[not(child::*)]/text()">

I'm adding this XSLT answer since it seems google's front matches lack such a solution:

After a long struggle with extracting CDATA as XML, eventually, this expression worked best for me:

<xsl:template match="*[not(child::*)]/text()">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文