deepak
deepak

Reputation: 339

Select distinct child elements from repeating parent using XPath

I want to select distinct child elements from repeating parent using XPath 1.0. Following is the 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>

Now, in the expected output I just want to pick the distinct catalog elements across the entire XML so that I can process it further. So the output should be:

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

I looked at some existing solutions, like not(. = ../following-sibling::*) but it is not helping me because I am trying to pick distinct items across repeating parent items. Please help and sorry for the outrageous xml.

Upvotes: 0

Views: 1075

Answers (1)

Jens Erat
Jens Erat

Reputation: 38682

Use the following axis instead of following-sibling, it will search all document starting from the current context:

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

Upvotes: 1

Related Questions