theanalyst
theanalyst

Reputation: 37

Get all preceding and following siblings for a given node

I have the following XML snippet:

<root>
    <CharacteristicUse>
        <UseArea>Specification_Characteristics</UseArea>
        <Value>
            <ValueID>123</ValueID>
        </Value>
        <Value>
            <ValueID>444</ValueID>
        </Value>
        <Value>
            <ValueID>555</ValueID>
        </Value>
        <Value>
            <ValueID>777</ValueID>
        </Value>
        <Value>
            <ValueID>888</ValueID>
        </Value>
    </CharacteristicUse>
</root>

I want to be able to get all preceding and following siblings of the Value node which has its child node ValueID with text = 555

I have been trying to combine the following way:

/root/CharacteristicUse/Value[ValueID='555']/following-sibling::* | preceding-sibling::*

But, it only returns to me the following siblings. Is it possible to have one single query to get the output as:

<Value>
   <ValueID>123</ValueID>
</Value>
<Value>
   <ValueID>444</ValueID>
</Value>
<Value>
   <ValueID>777</ValueID>
</Value>
<Value>
<ValueID>888</ValueID>
</Value>

Upvotes: 1

Views: 5063

Answers (2)

theanalyst
theanalyst

Reputation: 37

I found the answer. The xpath looks like:

/root/CharacteristicUse/Value[ValueID='555']/following-sibling::Value | /root/CharacteristicUse/Value[ValueID='555']/preceding-sibling::Value

Upvotes: 0

evil otto
evil otto

Reputation: 10582

One way is brute-force:

/root/CharacteristicUse/Value[ValueID='555']/preceding-sibling::* | /root/CharacteristicUse/Value[ValueID='555']/following-sibling::*

This includes the "UseArea" node also, not sure if you want that.

Or if what you want is just all the values that are NOT a particular value, then

/root/CharacteristicUse/Value[not(ValueID='555')]

is more direct.

Upvotes: 2

Related Questions