XPath:如何检查相似节点的多个属性

发布于 2024-09-04 13:17:58 字数 607 浏览 10 评论 0原文

如果我有一些像这样的xml:

    <root>
   <customers>
        <customer firstname="Joe" lastname="Bloggs" description="Member of the Bloggs family"/>
        <customer firstname="Joe" lastname="Soap" description="Member of the Soap family"/>
        <customer firstname="Fred" lastname="Bloggs" description="Member of the Bloggs family"/>
        <customer firstname="Jane" lastname="Bloggs" description="Is a member of the Bloggs family"/>
   </customers>
 </root>

如何在纯XPath(而不是XSLT)中获得一个xpath表达式来检测姓氏相同但具有不同描述的行?那么它会拉出上面的最后一个节点吗?

If I have some xml like:

    <root>
   <customers>
        <customer firstname="Joe" lastname="Bloggs" description="Member of the Bloggs family"/>
        <customer firstname="Joe" lastname="Soap" description="Member of the Soap family"/>
        <customer firstname="Fred" lastname="Bloggs" description="Member of the Bloggs family"/>
        <customer firstname="Jane" lastname="Bloggs" description="Is a member of the Bloggs family"/>
   </customers>
 </root>

How do I get, in pure XPath - not XSLT - an xpath expression that detects rows where lastname is the same, but has a different description? So it would pull the last node above?

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

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

发布评论

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

评论(2

七堇年 2024-09-11 13:17:58

如何获得纯 XPath - 而不是 XSLT
- 一个 xpath 表达式,用于检测姓氏相同的行,但是
有不同的描述吗?

以下是如何使用单个 XPath 表达式执行此操作

   "/*/*/customer
        [@lastname='Bloggs'
       and
        not(@description
           = preceding-sibling::*[@lastname='Bloggs']/@description
            )
        ]"

此表达式选择属性 lastname 等于“Bloggs”的所有 元素,并且属性description的不同值。

选定的节点是:

<customer firstname="Joe" lastname="Bloggs" description="Member of the Bloggs family"/>
<customer firstname="Jane" lastname="Bloggs" description="Is a member of the Bloggs family"/>

How do I get, in pure XPath - not XSLT
- an xpath expression that detects rows where lastname is the same, but
has a different description?

Here's how to do this with a single XPath expression:

   "/*/*/customer
        [@lastname='Bloggs'
       and
        not(@description
           = preceding-sibling::*[@lastname='Bloggs']/@description
            )
        ]"

This expression selects all <customer> elements with attribute lastname equal to "Bloggs" and different value of the attribute description.

The selected nodes are:

<customer firstname="Joe" lastname="Bloggs" description="Member of the Bloggs family"/>
<customer firstname="Jane" lastname="Bloggs" description="Is a member of the Bloggs family"/>
森林很绿却致人迷途 2024-09-11 13:17:58
/root/customers/customer[@lastname='Bloggs'
  and not(@description = preceding-sibling::*[@lastname='Bloggs']/@description)
  and not(@description = following-sibling::*[@lastname='Bloggs']/@description)]

不过,分步进行会表现得更好。

/root/customers/customer[@lastname='Bloggs'
  and not(@description = preceding-sibling::*[@lastname='Bloggs']/@description)
  and not(@description = following-sibling::*[@lastname='Bloggs']/@description)]

It would perform better doing it in steps, though.

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