I have below XML:
<?xml version="1.0" encoding="utf-8" ?>
<SLOCDETAILS>
<INPUT>
</INPUT>
<TABLES>
<OUTPUT>
<item>
<PLANT>INDIA</PLANT>
<SLOC>T150</SLOC>
<MATNR>220250</MATNR>
<CHARG>112233</CHARG>
</item>
<item>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<MATNR>220250</MATNR>
<CHARG>223344</CHARG>
</item>
<item>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<MATNR>220250</MATNR>
<CHARG>445566</CHARG>
</item>
<item>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<MATNR>220250</MATNR>
<CHARG>667788</CHARG>
</item>
<item>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<MATNR>220250</MATNR>
<CHARG>998877</CHARG>
</item>
</OUTPUT>
</TABLES>
</SLOCDETAILS>
My XSLT is as below:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan" version="1.0">
<xsl:template match="/">
<Rowsets>
<Rowset Name="List">
<xsl:for-each select="SLOCDETAILS/TABLES/OUTPUT/item">
<Row>
<PLANT>
<xsl:value-of select="PLANT"/>
</PLANT>
<SLOC>
<xsl:value-of select="SLOC"/>
</SLOC>
<Material>
<xsl:value-of select="MATNR"/>
</Material>
<Batch>
<xsl:value-of select="CHARG"/>
</Batch>
</Row>
</xsl:for-each>
</Rowset>
</Rowsets>
</xsl:template>
</xsl:stylesheet>
My resultant XML IS:
<?xml version="1.0" encoding="utf-8"?>
<Rowsets>
<Rowset Name="List">
<Row>
<PLANT>INDIA</PLANT>
<SLOC>T150</SLOC>
<Material>220250</Material>
<Batch>112233</Batch>
</Row>
<Row>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<Material>220250</Material>
<Batch>223344</Batch>
</Row>
<Row>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<Material>220250</Material>
<Batch>445566</Batch>
</Row>
<Row>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<Material>220250</Material>
<Batch>667788</Batch>
</Row>
<Row>
<PLANT>INDIA</PLANT>
<SLOC>T152</SLOC>
<Material>220250</Material>
<Batch>998877</Batch>
</Row>
</Rowset>
</Rowsets>
Now I want only SLOC that are unique. So for this XML, the SLOC that should be populated should be <SLOC>T150</SLOC>
& <SLOC>T152</SLOC>
<SLOC>T152</SLOC>
is being repeated 4 times. i want to avoid that.
How can I achieve that using XSLT?
I'm sure there is a grouping method that can be used. Also does XSLT provide distinct-value kind of function that we can plug & play? Different approaches to resolve this issue will be very helpful.
Thanks !
Give this a try:
<xsl:template match="/">
<Rowsets>
<Rowset Name="List">
<xsl:for-each select="SLOCDETAILS/TABLES/OUTPUT/item[not(SLOC = preceding-sibling::item/SLOC)]">
<Row>
<PLANT>
<xsl:value-of select="PLANT"/>
</PLANT>
<SLOC>
<xsl:value-of select="SLOC"/>
</SLOC>
<Material>
<xsl:value-of select="MATNR"/>
</Material>
<Batch>
<xsl:value-of select="CHARG"/>
</Batch>
</Row>
</xsl:for-each>
</Rowset>
</Rowsets>
</xsl:template>