[xsl] <xsl:sort> and <xsl:copy-of> in conjunction with one another

Subject: [xsl] <xsl:sort> and <xsl:copy-of> in conjunction with one another
From: "Stanger, Jan" <jan.stanger@xxxxxxxx>
Date: Tue, 28 Jan 2003 22:52:04 +0100
Truly sorry to bombard everyone with my source code mails, but I'm trying to fix a number of fundamental XSL code problems in the application that I am working on right now. Below XSL takes an input XML of the already mentioned form:

<DataSet>
  <ResultSetMetaData>
    <ColumnMetaData dtype="number" name="Quantity">
    <ColumnMetaData dtype="text" name="Description">
  </ResultSetMetaData>

  <DataRow type="detail">
    <column name="Quantity">255.00</column>
    <column name="Description">IBM</column>
  </DataRow>

  <DataRow type="detail">
    <column name="Quantity">100.00</column>
    <column name="Description">Sun Microsystems</column>
  </DataRow>
</DataSet>

...and then sorts it by up to five columns. The problem is that the sorting doesn't work correctly (anymore). This XSL used to look a lot like my previous (bad) example where the output method was 'text' and it would use a lot of &lt; and &gt; to construct the output XML. I now tried to refactor it using <xsl:copy> and <xsl:copy-of .../> instead, but that seems to have somehow broken the sorting. Also, for the variable assignments, it now uses relative paths whereas it was previously using absolute paths like "//DataSet/ResultSetMetaData/...", which I read is not very efficient.

Any help is much appreciated!
Jan

----------------- begin XSL code example -------------------

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
                
<xsl:strip-space elements="DataSet ResultSet ResultSetMetaData DataRow column"/>

<xsl:output method="xml" />

<!-- The sort parameters along with sortorder for different levels -->
<xsl:param name="sortby0" />
<xsl:param name="sortorder0" />
<xsl:param name="sortby1" />
<xsl:param name="sortorder1" />
<xsl:param name="sortby2" />
<xsl:param name="sortorder2" />
<xsl:param name="sortby3" />
<xsl:param name="sortorder3" />
<xsl:param name="sortby4" />
<xsl:param name="sortorder4" />

<xsl:template match="DataSet">
    <!-- Copy top DataSet node to result -->
    <xsl:copy>
    
        <!-- Copying entire meta data section into result XML -->
        <xsl:copy-of select="ResultSetMetaData" />

        <!-- The datatype is obtained from the XML metadata sction for all the sort levels. If no sort column is specified
             for a sort level, the data type is defaulted to "text".-->
        <xsl:variable name="sortdatatype0">
            <xsl:choose>
                <xsl:when test="$sortby0='none'">
                    <xsl:text>text</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="ResultSetMetaData/ColumnMetaData[@name=$sortby0]/@dtype" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="sortdatatype1">
            <xsl:choose>
                <xsl:when test="$sortby1='none'">
                    <xsl:text>text</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="ResultSetMetaData/ColumnMetaData[@name=$sortby1]/@dtype" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

[... AND SO ON FOR ALL OTHER SORT LEVELS ...]

        <!-- Actual sorting of data rows -->            
        <xsl:for-each select="DataRow">
            <xsl:sort select="column[@name=$sortby0]" data-type="{$sortdatatype0}" order="{$sortorder0}" />
            <xsl:sort select="column[@name=$sortby1]" data-type="{$sortdatatype1}" order="{$sortorder1}" />
            <xsl:sort select="column[@name=$sortby2]" data-type="{$sortdatatype2}" order="{$sortorder2}" />
            <xsl:sort select="column[@name=$sortby3]" data-type="{$sortdatatype3}" order="{$sortorder3}" />
            <xsl:sort select="column[@name=$sortby4]" data-type="{$sortdatatype4}" order="{$sortorder4}" />
            <!-- Finally copy each DataRow node to output XML -->                
            <xsl:copy-of select="." />            
        </xsl:for-each>

    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

--------- end of code example and begin of ugly company footer ----------------


This message is for the named person's use only. It may contain sensitive and private proprietary or legally privileged information. No confidentiality or privilege is waived or lost by any mistransmission. If you are not the intended recipient, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. CREDIT SUISSE GROUP and each legal entity in the CREDIT SUISSE FIRST BOSTON or CREDIT SUISSE ASSET MANAGEMENT business units of CREDIT SUISSE FIRST BOSTON reserve the right to monitor all e-mail communications through its networks. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity.
Unless otherwise stated, any pricing information given in this message is indicative  only, is subject to change and does not constitute an offer to deal at any price quoted. Any reference to the terms of executed transactions should be treated as  preliminary only and subject to our formal written confirmation.



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread