XPath - 测试属性值和文本值

a.h*_*die 2 xml xpath parsing xml-parsing

我正在尝试编写一个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>
Run Code Online (Sandbox Code Playgroud)

我想检查是否存在@ name ="addonSection.product"和value == ProPlus的字段元素.

我曾尝试使用以下XPath,但每次都返回null.

//field[@name = 'addonSection.product']/[text()='ProPlus']
Run Code Online (Sandbox Code Playgroud)

任何评论欢迎

谢谢

ale*_*cxe 5

你不需要/在这些条件之间.加入您的支票and:

//field[@name='addonSection.product' and text()='ProPlus']
Run Code Online (Sandbox Code Playgroud)

演示(使用xmllint):

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