XPath:如何跨类似节点检查多个属性

时间:2010-06-09 17:20:08

标签: xpath attributes match

如果我有一些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相同但具有不同描述的行?那么它会将最后一个节点拉到上面吗?

3 个答案:

答案 0 :(得分:1)

  

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

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

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

此表达式选择属性<customer>等于“Bloggs”的所有lastname元素以及属性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"/>

答案 1 :(得分:0)

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

尽管如此,它会更好地完成它。

答案 2 :(得分:0)

嘿伙计们,谢谢,那就是天才!

但是,如果我只是针对Bloggs系列,那么它可以正常工作,但是是否可以对此进行概括,以便它适用于姓氏相同但描述不同的任何节点?

此外,是否可以在树上进一步引用的节点上进行此工作,请说明以下内容:

<root>
   <customers index="1">
        <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>
   <customers index="2">
        <customer firstname="J" lastname="Bloggs" description="Member of the Bloggs family"/>
        <customer firstname="J" lastname="Soap" description="Is a member of the Soap family"/>
        <customer firstname="J" lastname="Smith" description="Member of the Smith family"/>
   </customers>
   <customers index="2">
        <customer firstname="J" lastname="Smith" description=""/>
        <customer firstname="A" lastname="Soap" description="Member of the Smith family"/>
   </customers>
</root>

所以我想拉出以下节点:

Jane Bloggs in index=1
J Soap in index=2
J Smith in index=3

如果你可以提供帮助,请再次欢呼,希望我不要推动它;)