XPath -测试属性值和文本值



我试图编写一个Xpath条件,如果元素的属性和值是正确的,则返回该元素,否则返回null。

这是我的xml

<record>
  <field name="uid">ABC70019469</field>
  <field name="effectiveDate">10 May 2014 00:00:00 BST</field>
  <field name="expiryDate">09 May 2015 23:59:59 BST</field>
  <field name="price">48.49</field>
  <field name="cancelled">{cancelled}</field>
  <field name="addonSection.effectiveStartTime">09 May 2014 09:04:29 BST</field>
  <field name="addonSection.effectiveEndTime">31 December 9999 23:59:59 GMT</field>
  <field name="addonSection.brand">BIT</field>
  <field name="addonSection.product">ProPlus</field>
<record>

我想检查是否有一个字段元素与@name="addonSection。product"和value == ProPlus。

我尝试使用下面的XPath,但是每次都返回null。

//field[@name = 'addonSection.product']/[text()='ProPlus']

欢迎评论

谢谢

两个条件之间不需要/。使用and连接您的支票:

//field[@name='addonSection.product' and text()='ProPlus']

Demo (using xmllint):

$ xmllint input.xml --xpath "//field[@name='addonSection.product' and text()='ProPlus']"
<field name="addonSection.product">ProPlus</field>

最新更新