views:

135

answers:

1

Suppose I have this XML:

<x>
<e s="1" t="A"/>
<e s="2" t="A"/>
<e s="1" t="B"/>
</x>

Is there any way to write an xpath to find whether there are two distinct nodes named "e" which have the same value for @s but different values of @t. The first part is easy:

//e[@s = //e/@s] 

as is the second part:

//e[@t != //e[@t]]

But I don't see any way to construct an xpath that compares two different attributes for two separate elements "e". Is there a way within the xpath syntax, or is it hopeless?

+1  A: 

The requested nodes cannot be selected by a single XPath 1.0 expression.

If the hosting language is XSLT 1.0, then an expression using the current() function can be constructed, that selects the desired nodes.

Use the following XPath 2.0 expression:

for $x
           in
             /*/e[@s
                 = (preceding-sibling::e
                   |
                    following-sibling::e
                    )
                     /@s
                ],

           $y in /*/e[@s = $x/@s]

         return
            $x[not(@t = $y/@t)]
Dimitre Novatchev
Probably I should clarify a bit. The XPath above will match the third element in this XML: <x> <e s="2" t="B"/> <e s="1" t="A"/> <e s="1" t="A"/> </x>When it shouldn't. It matches e[2]/@s with e[1]/@s, and matches e[2]/@t with e[0]/@t; But neither e[0] nor e[1] match both conditions. I'm really wondering if there is a way to only select on e[x] if e[y] fulfills both of the matching criteria.
Yeah, I was looking for an XPath 1.0 answer...but at least I know it can't be done and can stop butting my head against the problem!