Peter
Peter

Reputation: 1796

XSLT 1.0: sort unique values with key but with exceptions

I have the following simplified XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<DataSet>
    <Tables>
        <Table>
            <Rows>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9991</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9992</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9994</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9995</revenueCenterPOSReflongtrue>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue/>
                </R>
                <R>
                    <transactionQualifierstring>Sales</transactionQualifierstring>
                    <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
                </R>
            </Rows>
        </Table>
    </Tables>
</DataSet>
</ExportData>

I need to group the recordset (R) depending on the "Qualifierstring" and the "revenueCenter". I came up with this XSLT which works perfectly:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>
<xsl:template match="text()|@*"/>

<!-- define the key to define unique elements -->
<xsl:key name="keyTranstypeCcRc" match="R" use="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>

<!-- define which elements are unique -->
<xsl:template match="ExportData/DataSet/Tables/Table/Rows">
    <xsl:variable name="uniqueTransactions" select="R[generate-id()=generate-id(key('keyTranstypeCcRc',concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))[1])]"/>
    <ExportData>
        <xsl:apply-templates select="$uniqueTransactions" mode="group"/>
    </ExportData>
</xsl:template>

<!-- create the unique groups -->
<xsl:template match="R" mode="group">
    <transaction>
        <xsl:attribute name="transactionType"><!-- write the 2 key criteria into this attribute -->
            <xsl:value-of select="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>
        </xsl:attribute>
        <xsl:apply-templates select="key('keyTranstypeCcRc', concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))" mode="item"/>
    </transaction>
</xsl:template>

<!-- write the item content into each group -->
<xsl:template match="R" mode="item">
    <R>
        <xsl:copy-of select="child::*"/> 
    </R>
</xsl:template>

</xsl:stylesheet>

I define a key and select unique items, build the groups and process the recordsets (R).

But now how could I define the key so I am grouping "revenueCenter" 9992 and 9993 together? Do I have to set up 2 different keys? (but how could I use 2 keys with my templates) Or can I extend the existing key with values?

The XML should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<transaction transactionType="Sales|9991">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9991</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9993 Sales|9992">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9992</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9994">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9994</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9995">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9995</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue/>
    </R>
</transaction>
</ExportData>

24th May 2012 UPDATE: I actually came up with an XSLT that does what I want, I am using two different keys:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>
<xsl:template match="text()|@*"/>


<!-- define the key to define unique elements -->
<!--<xsl:key name="keyTranstypeCcRc" match="R" use="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>-->
<xsl:key name="keyTranstypeCcRc" match="R[not(revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993')]" 
    use="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>

<xsl:key name="keyTranstypeCcRc99923" match="R[revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993']" 
    use="transactionQualifierstring"/>

<!-- define which elements are unique -->
<xsl:template match="ExportData/DataSet/Tables/Table/Rows">
    <xsl:variable name="uniqueTransactions" select="R[not(revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993')]
        [generate-id()=generate-id(key('keyTranstypeCcRc',concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))[1])]"/>
    <xsl:variable name="keyTranstypeCcRc99923" select="R[revenueCenterPOSReflongtrue='9992' or revenueCenterPOSReflongtrue='9993']
        [generate-id()=generate-id(key('keyTranstypeCcRc99923',transactionQualifierstring)[1])]"/>
    <ExportData>
        <xsl:apply-templates select="$uniqueTransactions" mode="group"/>
        <xsl:apply-templates select="$keyTranstypeCcRc99923" mode="group99923"/>
    </ExportData>
</xsl:template>

<!-- create the unique groups -->
<xsl:template match="R" mode="group">
    <transaction>
        <xsl:attribute name="transactionType"><!-- write the 2 key criteria into this attribute -->
            <xsl:value-of select="concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue)"/>
        </xsl:attribute>
        <xsl:apply-templates select="key('keyTranstypeCcRc', concat(transactionQualifierstring,'|',revenueCenterPOSReflongtrue))" mode="item"/>
        
    </transaction>
</xsl:template>

<!-- create the unique groups -->
<xsl:template match="R" mode="group99923">
    <transaction>
        <xsl:attribute name="transactionType"><!-- write the 2 key criteria into this attribute -->
            <xsl:value-of select="concat(transactionQualifierstring,'|','9992','|','9993')"/>
        </xsl:attribute>
        <xsl:apply-templates select="key('keyTranstypeCcRc99923', transactionQualifierstring)" mode="item"/>
        
    </transaction>
</xsl:template>

<!-- write the item content into each group -->
<xsl:template match="R" mode="item">
    <R>
        <xsl:copy-of select="child::*"/> 
    </R>
</xsl:template>

</xsl:stylesheet>

Applied to the source XML I get this correct output:

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<transaction transactionType="Sales|9991">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9991</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9994">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9994</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|9995">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9995</revenueCenterPOSReflongtrue>
    </R>
</transaction>
<transaction transactionType="Sales|">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue/>
    </R>
</transaction>
<transaction transactionType="Sales|9992|9993">
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9992</revenueCenterPOSReflongtrue>
    </R>
    <R>
        <transactionQualifierstring>Sales</transactionQualifierstring>
        <revenueCenterPOSReflongtrue>9993</revenueCenterPOSReflongtrue>
    </R>
</transaction>
</ExportData>

This solution works because I know which values I need to exclude and group seperately.

Upvotes: 0

Views: 813

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86744

Any time you want things sorted and/or grouped in a particular manner it helps to think of generating a "sort key". At the point where you generate the index

<xsl:key name="keyTranstypeCcRc" match="R" use="makeKey(...)"/>

you need to provide a function that generates the desired key from the appropriate arguments. You then use the same function when looking up keys in the index.

In XSLT 1 you could use the EXSLT func:function to package the logic reusably.

If you're familiar with Java, this is kind of like defining a custom Comparator.

Upvotes: 1

Related Questions