【问题标题】:How to use condtion in xpath [duplicate]如何在xpath中使用条件[重复]
【发布时间】:2018-05-17 09:02:16
【问题描述】:
<courses>
<course number = "1" category = "A">
<title>B</title>
</course>
</courses>

我想选择所有 @number 的 category="A" 我尝试使用 //@number[@category/text()='A'],但似乎没有用, 那我怎样才能得到它

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    您的 XML 无效,您需要在 1 两边加上引号:

    <course number="1" category = "A">
    

    对于属性值,您永远不需要text()。使用

    //@number[../@category="A"]
    

    意思是:

    • //@number:选择任意数字属性
    • [...]: 满足条件
    • ../:上到父级(元素course或其他)
    • @category:找一个类别属性
    • ="A":其值为A

    【讨论】:

    • 谢谢,我会解决的
    【解决方案2】:

    category='A'获取&lt;course&gt;节点的@number属性值:

    //course[@category='A']/@number
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 2013-06-14
      相关资源
      最近更新 更多