Given the sample XML data:
<root>
<a>
<id>1</id>
<status>on</status>
<level>4</level>
<otherData>asdfasdfsdfvasdF</otherData>
<b>
<name>A1</name>
</b>
</a>
<a>
<id>2</id>
<status>on</status>
<level>8</level>
<otherData>asdfasdfsdfvasdF</otherData>
<b>
<name>TEST</name>
</b>
</a>
<a>
<id>3</id>
<status>off</status>
<level>2</level>
<otherData>asdfasdfsdfvasdF</otherData>
<b>
<name>A1</name>
</b>
</a>
</root>
I’d like to write an XPath expression that will evaluate to:
1 on 3 off
That is, I’d like to extract the text of the id node and concatenate it with the status node. There is an additional condition: I only want to do this for cases where the name is “A1”. The XPath expression to just get the id is this:
//name[text()="A1"]/../../id/text()
And here’s an attempt to concatenate id with it’s sibling status, which does not work.
//name[text()="A1"]/../../concat(id/text() , status/text())
Is what I’m trying to do even possible? And if so, what would be the correct XPath expression?
To clarify: I’m not doing any transformations with this expression, I’m just plugging it into an XPath expression evaluator (such as in Notepad++ or JEdit – via plugins) to help debug subtle data errors in very large XML files. In this example, certain combinations of id and status might not be correct if the b child element has name="A1", so I am trying to find them all with one simple expression rather than scrolling through the file by hand.
XPath 2.0:
Result:
Tested in Oxygen XML Editor, a very good tool IMHO.