XPATH保留标记

时间:2015-05-19 12:01:16

标签: xml xpath tags

嗨我有一个像这样的xml:

<root>
    <parent1>
        <leaf1>value1</leaf1>
        <leaf2>value1</leaf2>
        <leaf3>value1</leaf3>
        <leaf4>value1</leaf4>
    </parent1>
    <parent2>
        <leaf1>value1</leaf1>
        <leaf2>value1</leaf2>
        <leaf3>value1</leaf3>
        <leaf4>value1</leaf4>
        ...
        <leafN>value1</leafN>
    </parent2>
</root>

我需要将其作为输出提取:

<leaf1>value1</leaf1>
<leaf2>value1</leaf2>
<leaf3>value1</leaf3>
<leaf4>value1</leaf4>
...
<leafN>value1</leafN>

我怎么写和xpath查询让我有可能跳过一些节点...例如:leaf1和leaf3?

2 个答案:

答案 0 :(得分:1)

如果XML就像问题一样简单,要提取所有root的孙子,你可以使用:

/*/*/*

要排除leaf1leaf3,您可以使用以下内容:

/*/*/*[not(self::leaf1) and not(self::leaf3)]

答案 1 :(得分:0)

如果要查找不依赖于树的结构的所有叶节点(不包括某些节点),请使用xpath:

//*[count(*)=0 and not(self::leaf1) and not(self::leaf3)]