Tuesday, February 7, 2017

Get distinct node from Xml

Ques  - An xml contains duplicate nodes with same attribute value. Write some xslt code to get the distinct node,

Sample Xml
<?xml version="1.0" encoding="UTF-8"?>
<Doc>
    <Node name="A">REDfgdg</Node>
    <Node name="A">RED</Node>
    <Node name="B"/>
    <Node name="B"/>
    <Node name="C"/>
    <Node name="C"/>
</Doc>

Ans:

Xslt

       <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
    <xsl:template match="nodes">
        <xsl:for-each-group select="node" group-by="@name">
            <xsl:value-of select="current-grouping-key()"/>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

           
       
   

No comments:

Followers

Link