Question

I am trying to write an Xpath condition that returns the element if its attribute and value are correct, otherwise return null.

Here is my 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>

I want to check if there is a field element with @name="addonSection.product" and value == ProPlus.

I have tried to use the following XPath, but it returns null every time.

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

Any comments welcome

Thanks

Was it helpful?

Solution

You don't need / between the conditions. Join your checks with an 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top