I have an xml like;
<a>
<b att="x">
</b>
</a>
I parse this xml with java and i have a node b in my hand.My question is how can i convert this node to a xpath expression a/b[@att='x']
?
I have an xml like;
<a>
<b att="x">
</b>
</a>
I parse this xml with java and i have a node b in my hand.My question is how can i convert this node to a xpath expression a/b[@att='x']
?
I don't think there is an absolutely generic solution for this. In xpath 2.0 you could probably make something like
string-join(
for $x in ./ancestor-or-self::* return
if ($x/@att)
then concat($x/name(), '[@att="', $x/@att, '"]')
else $x/name()
, '/'
)
For input
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a>
<b att="x"></b>
<b att="y"></b>
<b att="z"></b>
</a>
</root>
supposing you are holding 3rd b element it gives string
root/a/b[@att="z"]
But it's very limited in this form (it look only for one attribute, this attribute has to be named "att", it doesn't work with position in case of multiple elements b with same @att value, etc. etc.) and has to be perhaps written in more precise form. It is just an idea.