Anonymoose
Anonymoose

Reputation: 2471

XPath get node like

I'm quite challenged with the following problem.

My XML contains several orders with a status. They are grouped together by appending integers to the tags. It would be easier if each order had it's own parent tag but this is the way I get the XML from an external application and I cannot change it.

<Values>
    <ORDER_1>506137</ORDER_1>
    <STATUS_1>3</STATUS_1>

    <ORDER_2>506129</ORDER_2>
    <STATUS_2>4</STATUS_2>

    <ORDER_3>51893</ORDER_3>
    <STATUS_3>1</STATUS_3>
</Values>

What I need to do is find the order that corresponds to status '4'. Is it even possible with XPath? It seems like I need a wildcard to select the status, but even then how do I find the corresponding order?

FYI: XPath 1.0

Upvotes: 1

Views: 50

Answers (1)

har07
har07

Reputation: 89285

You can try to filter node name using XPath name() and starts-with() function, then use preceding-sibling to select corresponding order element, for example :

//Values/*[starts-with(name(), 'STATUS_')][.=4]/preceding-sibling::*[1]

Above XPath will return this node :

<ORDER_2>506129</ORDER_2>

Upvotes: 3

Related Questions