Home > Mobile >  Check if xml element of array has exact values with Xpath
Check if xml element of array has exact values with Xpath

Time:10-25

can you tell me how to check if my xml contains nested element in array with exact values? Here is my sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item>
      <identifier>100</identifier>
      <value>ABC</value>
   </item>
   <item>
      <identifier>200</identifier>
      <value>XYZ</value>
   </item>
   <item>
      <identifier>100</identifier>
      <value>DEF</value>
   </item>
   <item>
      <identifier>300</identifier>
      <value>GFH</value>
   </item>
</items>

For instance, I need check if xml above contains element with identifier = 100 and element with this identifier must has value = DEF. It could be on any index in array items.

CodePudding user response:

You can use this XPath expression

"//items[.//identifier='100'][.//value='DEF']"

Or this

"//items[.//identifier='100' and .//value='DEF']"

CodePudding user response:

Testing with XPath could be written like this:

boolean(/items/item[./identifier='100' and ./value='DEF'])
  • Related