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

Wix Custom Actions with WixUI_Minimal

installer wix windows-installer custom-action

So, I'm trying to, after an application finishes installing (though in truth, it really doesn't matter when the secondary application is run, since it doesn't interact with the installed files during

How preg_match works exactly?

php regex validation

I've wrote a simple function to check if the string I send "should be" valid or not.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.