xsl-list
[Top] [All Lists]

Re: Re: attribute values as name value pair

2004-02-27 01:34:57

Here's my second go at it. I just had to see if I could figure out a
way to count only the rows which matched our criteria.  Anyone see any
problems with it?  How would one test whether the following solution
is efficent or inefficent compared to the much shorter and to-the-point
example Sam posted?

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

    <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

    <xsl:template match="/">
        <data>
            <xsl:apply-templates select="//data/row/@*"/>
        </data>
    </xsl:template>

    <!--
       Turn each row attribute into an element row with
       attributes id, key, and value.
    -->
    <xsl:template match="row/@*">
        <xsl:variable name="name" select="name()"/>
        <xsl:variable name="disp" 
select="//table/columns/column[(_at_)name=$name]/@value"/>
        <xsl:if test="$disp">
            <xsl:element name="row">
                <xsl:attribute name="id">
                    <xsl:call-template name="row_count">
                        <xsl:with-param name="rows" select="//data/row/@*"/>
                        <xsl:with-param name="id" select="generate-id(.)"/>
                        <xsl:with-param name="idx_count" select="1"/>
                        <xsl:with-param name="match_count" select="1"/>
                    </xsl:call-template>
                </xsl:attribute>
                <xsl:attribute name="key">
                    <xsl:value-of select="$disp"/>
                </xsl:attribute>
                <xsl:attribute name="value">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </xsl:element>
        </xsl:if>
    </xsl:template>

    <!--
       count the number of $rows nodes which occur
       up to the node with a generate-id() matchin $id,
       and which have a name() equal to one in
       /table/columns/column/@name
    -->
    <xsl:template name="row_count">
        <xsl:param name="rows"/>
        <xsl:param name="id"/>
        <xsl:param name="idx_count"/>
        <xsl:param name="match_count"/>

        <xsl:variable name="new_idx_count" select="1+$idx_count"/>

        <xsl:variable name="name" select="name($rows[$idx_count])"/>
        <xsl:variable name="match" 
select="count(//table/columns/column[(_at_)name=$name])"/>

        <xsl:choose>
            <xsl:when test="generate-id($rows[$idx_count]) = $id">
                <xsl:value-of select="$match_count"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="row_count">
                    <xsl:with-param name="rows" select="$rows"/>
                    <xsl:with-param name="id" select="$id"/>
                    <xsl:with-param name="idx_count" select="$new_idx_count"/>
                    <xsl:with-param name="match_count" 
select="$match+$match_count"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

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



<Prev in Thread] Current Thread [Next in Thread>