获取给定节点的所有前后兄弟节点 [英] Get all preceding and following siblings for a given node

查看:38
本文介绍了获取给定节点的所有前后兄弟节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 片段:

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>

我希望能够获取 Value 节点的所有前后兄弟节点,该节点的子节点 ValueID 为 text = 555

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>

推荐答案

一种方法是蛮力:

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

这也包括UseArea"节点,不确定是否需要.

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')]

更直接.

这篇关于获取给定节点的所有前后兄弟节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆