0
votes

I need to filter operation in some of the nodes which are duplicate (on a key) Following are my data file.

data.xml

<root>
    <node name="item1" />
    <node name="item2" />
    <node name="item3" />
    <node name="item4" />
</root>

in file item1.xml

<item>
 <group>A</group>
</item>

item2.xml

<item>
 <group>B</group>
</item>

item3.xml

<item>
 <group>B</group>
</item>

item4.xml

<item>
 <group>D</group>
</item>

XSLT File

<xsl:for-each select="/root/node">
     <xsl:variable name="itemName"   select="@name"/>
     <xsl:variable name="groupName"  select="document($itemName)/item/group"/>
     <xsl:value-of select="concat('Group ',$groupName)"/>
</xsl:for-each>   

output

Group A Group B Group B Group C

Desired output

Group A Group B Group C

Here item 2 and 3 are of same group according to their group attribute so I have to only print the group name of any of them.

1
I don't think the <xsl:key> method works cross-document, but perhaps that's part of the answer.cbeer
@cbeer Can anything be done if I put the attributes in the single file ? e.g <root> <node name="item1" group="A" />Neil

1 Answers

0
votes
<root>
    <node name="item1" group="A"/>
    <node name="item2" group="B"/>
</root>

If the group name itself can be somehow put in the main XML file as an attribute the distinct nodes can be found as follows

   <xsl:for-each select="/root/node[not(@group = preceding-sibling::node/@group)]">
   </xsl:for-each>

Above select expression would ignore any node which has been encountered earlier (belongs to one of the preceding nodes)

PS: This is not the solution the question which involves multiple XML files. Any answer in that line would be useful.