1

I want to use XPath to select a few nodes that end with the same word and check if they all equal one another. So, for example,

<a>
    <b-token>123456789</b-token>
    ...
    <c>
        <d>
            <d-token>123456789</d-token>
            ...
        </d>
        <e>
            <e-token>123456789</e-token>
            ...
        </e>
        <f>
            <f-token>123456789</f-token>
            ...
        </f>
    <c>
</a>

So, I want to search for all nodes ending in "-token" and make sure they are equivalent. Can this be done in XPath 1.0? 2.0? XSD?

Here is an example XPath that I though would have worked, but it doesn't

String xpathExpression = "/descendant-or-self::node()/*[substring(name(), string-length(name() ) - (string-length('token')-1) ) = 'token'] = /descendant-or-self::node()/*[substring(name(), string-length(name() ) - (string-length('token')-1) ) = 'token']";

It works when I put a specific value after the '=' like so:

String xpathExpression = "/descendant-or-self::node()/*[substring(name(), string-length(name() ) - (string-length('token')-1) ) = 'token'] = '123456789'";

I am using Java 7 if it matters.

Thanks

jediwompa
  • 79
  • 1
  • 11

1 Answers1

1

If you use not(//*[substring(local-name(), string-length(local-name()) - 5) = '-token'] != //*[substring(local-name(), string-length(local-name()) - 5) = '-token']) then that comparison gives true as long as all those elements have the same string value and false if there is at least one element whose string contents is not equal.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • It seems to me that this works; however, I am not following why. It appears you simply did the inverse of what I did which is to compare if the two sets of nodes are `!=` and then you take the opposite of that boolean value via wrapping everything in `not()`. To me they are logically the same, but apparently they are not. – jediwompa Apr 26 '16 at 18:15
  • With XPath, the comparison `$node-set1 != $node-set2` is true if at least one item in `$node-set1` is not equal to (at least) one item in `$node-set2`. And with the negation `not($node-set1 != $node-set2)` we ensure that all items are equal. See https://www.w3.org/TR/xpath/#booleans which says "If both objects to be compared are node-sets, then the comparison will be true if and only if there is a node in the first node-set and a node in the second node-set such that the result of performing the comparison on the string-values of the two nodes is true.". – Martin Honnen Apr 26 '16 at 18:21
  • OK, Thanks for the thorough explanation! I would upvote, but I am a lowly peon at the moment. – jediwompa Apr 26 '16 at 18:24
  • And your attempt with `=` is true as soon as one item in the first node-set is equal to one item in the second node-set. – Martin Honnen Apr 26 '16 at 18:24