I...">

XPath to compare two different attributes of two elements

Suppose I have this XML:

<x> <es="1" t="A"/> <es="2" t="A"/> <es="1" t="B"/> </x> 

Is there a way to write xpath to determine if there are two different nodes named "e" that have the same value for @s but different values โ€‹โ€‹of @t. The first part is simple:

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

like the second part:

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

But I see no way to build an xpath that compares two different attributes for two separate "e" elements. Is there a way in xpath syntax or is it hopeless?

+4
source share
1 answer

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

If the hosting language is XSLT 1.0, then an expression can be created using the current() function, which 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)] 
+2
source

Source: https://habr.com/ru/post/1312954/


All Articles