0
votes

I have some XML and I need to query all <USER> nodes, specifically if the <userID> values are all the same. The node I want to check is userID. In the below example notice that the 2 <userID> values are different, so I would want the result of the XPath evaluation to be false.

Sample XML:

<xml>
<CHANGE_USER_001>
     <USER>
        <userID>joebloggs</userID>
     </USER>
</CHANGE_USER_001>
<CHANGE_USER_001>
     <USER>
        <userID>joebloggs1</userID>
     </USER>
</CHANGE_USER_001>
</xml>

My attempt at xpath command is

//USER/userID='joebloggs'

The query returns true but I would like to test against each <userID> node.

If returning a boolean is not possible then I could get the number of <userID> nodes, and then get the number of <userID> rows equal to my specific value, and then compare them, but how to check all <userID> nodes for the same value?

1
The question isn't clear! What do you mean by if the values are all the same? Which nodes do you want to query? - Lingamurthy CS
I want to query userID node and check if they are the same value - andrewb
Can there be more than 2 users? Can the elements named CHANGE_USER_001 and CHANGE_USER_002 have different names? - JerryM
Multiple users are possible, I just skimmed down the XML. XML node names will not change - andrewb

1 Answers

2
votes

This xpath, not(//userID != //userID)

, returns false when at least one userID is different than any other userID in the whole document. It returns true when all the userIDs are the same.

The != operator returns true if the items on the right side of the expression have at least 1 value that doesn't match at least one of the items on the left side, else it returns false.