使用XPath从重复父项中选择不同的子元素

时间:2013-06-12 00:44:42

标签: xml xpath unique

我想使用XPath 1.0从重复父级中选择不同的子元素。 以下是XML:

<parent>
<something>
    <catalog>
        <title>AAA</title>
        <artist>111</artist>
    </catalog>
    <catalog>
        <title>AAA</title>
        <artist>111</artist>
    </catalog>
    <catalog>
        <title>BBB</title>
        <artist>222</artist>
    </catalog>
</something>
<something>
    <catalog>
        <title>CCC</title>
        <artist>333</artist>
    </catalog>
    <catalog>
        <title>BBB</title>
        <artist>222</artist>
    </catalog>
    <catalog>
        <title>CCC</title>
        <artist>333</artist>
    </catalog>
</something>
<something>
    <catalog>
        <title>AAA</title>
        <artist>111</artist>
    </catalog>
    <catalog>
        <title>BBB</title>
        <artist>222</artist>
    </catalog>
    <catalog>
        <title>CCC</title>
        <artist>333</artist>
    </catalog>
</something>
</parent>

现在,在预期的输出中,我只想在整个XML中选择不同的catalog元素,以便我可以进一步处理它。所以输出应该是:

<catalog>
    <title>AAA</title>
    <artist>111</artist>
</catalog>
<catalog>
    <title>BBB</title>
    <artist>222</artist>
</catalog>
<catalog>
    <title>CCC</title>
    <artist>333</artist>
</catalog>

我查看了一些现有的解决方案,例如not(. = ../following-sibling::*),但它没有帮助我,因为我试图在重复的父项目中选择不同的项目。请帮助并抱歉这个令人发指的xml。

1 个答案:

答案 0 :(得分:1)

使用以下轴而不是follow-sibling,它将从当前上下文开始搜索所有文档:

//catalog[not(. = following::catalog)]