I am fairly new to XPATH and need to return a value from multiple XML objects when a certain node is equal to one of two values. The solution also needs to be namespace agnostic using local-name(). Given the following XML, I am looking to return all of the values of “B” where “A” is equal to either “4” or “5”.
<?xml version="1.0"?>
<element>
<A>3</A>
<B>Value 1</B>
</element>
<element>
<A>4</A>
<B>Value 2</B>
</element>
<element>
<A>5</A>
<B>Value 3</B>
</element>
I have tried many iterations of the following expression but cannot seem to get the syntax correct when I try to add the second value for “A”. I just can’t seem to figure out the correct syntax for an “or” expression. I have also considered the use of a union and couldn’t get that syntax correct either.
This works for when “A” equals a single value of “4” but how to I add the test for a value of “5” so that the result returns two values for “B” (“Value 2” and “Value 3”)?
//*[local-name()='element'][*[local-name()='A']='4']/*[local-name()='B']/text()
Thank you. Any help would be very much appreciated!
Try this:
This expression selects
Value 2andValue 3.