Grouping of xml nodes in xslt 1.0

Input :

Enter XML

<Root>
    <Number>1</Number>
    <Reference>100</Reference>

    <Number>2</Number>
    <Reference>101</Reference>

    <Number>3</Number>
    <Reference>100</Reference>

    <Number>4</Number>
    <Reference>102</Reference>

    <Number>5</Number>
    <Reference>100</Reference>
</Root>

Expected Output :

<Root>
    <Number>1</Number>
    <Reference>100</Reference>
    <RefNumber>1</RefNumber>

    <Number>2</Number>
    <Reference>101</Reference>
    <RefNumber>1</RefNumber>

    <Number>3</Number>
    <Reference>100</Reference>
    <RefNumber>2</RefNumber>

    <Number>4</Number>
    <Reference>102</Reference>
    <RefNumber>1</RefNumber>

    <Number>5</Number>
    <Reference>100</Reference>
    <RefNumber>3</RefNumber>
</Root>

How to do Grouping based on Root/Reference and add sequential number to RefNumber in output in xslt 1.0?

Thanks in Advance

Answers


One way to do this is use xsl:number. Whenever you match a Reference element, copy the element, and add a RefNumber element with the number of Reference elements with the same value:

<xsl:template match="Reference">
    <xsl:copy-of select="." />
    <xsl:variable name="Ref" select="." />
    <RefNumber><xsl:number count="Reference[. = $Ref]" /></RefNumber>
</xsl:template>

Here is the full XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="Reference">
        <xsl:copy-of select="." />
        <xsl:variable name="Ref" select="." />
        <RefNumber><xsl:number count="Reference[. = $Ref]" /></RefNumber>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

When applied to your sample XML, the following is output:

<Root>
    <Number>1</Number>
    <Reference>100</Reference>
    <RefNumber>1</RefNumber>

    <Number>2</Number>
    <Reference>101</Reference>
    <RefNumber>1</RefNumber>

    <Number>3</Number>
    <Reference>100</Reference>
    <RefNumber>2</RefNumber>

    <Number>4</Number>
    <Reference>102</Reference>
    <RefNumber>1</RefNumber>

    <Number>5</Number>
    <Reference>100</Reference>
    <RefNumber>3</RefNumber>
</Root>

Do note the use of the identity transform template to copy other existing nodes.


Need Your Help

How to perform this query on a hash in a MongoDB document (using Mongoid)

mongodb mongoid

I am using Mongoid. I have a document that looks like the following: