1

如果我有一些 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 表达式,该表达式检测 lastname 相同但描述不同的行?那么它会拉出上面的最后一个节点吗?

4

2 回答 2

1

我如何在纯 XPath(而不是 XSLT)中获得一个 xpath 表达式,该表达式检测 lastname 相同但描述不同的行?

以下是使用单个 XPath 表达式执行此操作的方法

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

此表达式选择属性等于“Bloggs”且属性值不同的所有<customer>元素。lastnamedescription

选择的节点是:

<customer firstname="Joe" lastname="Bloggs" description="Member of the Bloggs family"/>
<customer firstname="Jane" lastname="Bloggs" description="Is a member of the Bloggs family"/>
于 2010-06-10T02:30:34.490 回答
0
/root/customers/customer[@lastname='Bloggs'
  and not(@description = preceding-sibling::*[@lastname='Bloggs']/@description)
  and not(@description = following-sibling::*[@lastname='Bloggs']/@description)]

不过,分步执行会更好。

于 2010-06-10T02:51:40.377 回答