[xsl] Grouping using distinct-values

Subject: [xsl] Grouping using distinct-values
From: laker.gold@xxxxxxxxx
Date: Thu, 3 Jul 2008 15:30:18 -0700
Hi all,

Struggling with my understanding of grouping (and email clients...),
in spite of the excellent examples in the FAQ and archives -- sorry
for my thick-headedness.

Using xslt2 and saxon 9

XML is a terrible db dump:

    <table name="tbl_nm>
        <column name="property_section_nm">[SECTION NAME 9]</column>
        <column name="property_type_id">10</column>
        <column name="property_type_nm">Common Property</column>
        <column name="property_nm">SERVICEURL_PRIMARY</column>
        <column name="sort_order">2</column>
        <column name="property_value">https://x.y.z.net/sd</column>
        <column name="property_section_nm">[SECTION NAME 2]</column>
        <column name="property_type_id">14</column>
        <column name="property_type_nm">some Property</column>
        <column name="property_nm">some name</column>
        <column name="sort_order">4</column>
        <column name="property_value">some value</column>
        <column name="property_section_nm">[SECTION NAME 9]</column>
        <column name="property_type_id">1</column>
        <column name="property_type_nm">random property</column>
        <column name="property_nm">another name</column>
        <column name="sort_order">4</column>
        <column name="property_value">another value</column>
    <!-- many more records ... -->
    <table name="tbl_nm2">
        <!-- many more records ... -->
    <!-- many more tables ... -->

I need to write name=value pairs (name being the <column
name="property_nm"> and value being the <column name="property_value">
for all groups of column elements (or is it groups of record
elements??) in table name="tbl_nm" which share a like value for
<column name="property_section_nm">, sorted by the value of the column
group's <column name="sort_order">.

So, the expected result for the above xml would be something like:

another name=another value
some name=some value

My thinking has been that I'll write a for loop which will identify
all the distinct property_section_nm that I have, and pass each of the
distinct values in turn to a call-template which can then group across
the xml doc, and, in a for loop of it's own, sort and then output the
name=value pairs:

<xsl:for-each select="distinct-values(record/column[@name='property_section_nm'])">
    <xsl:variable name="name" select="."/>
    <xsl:value-of select="$name"/>
    <xsl:call-template name="fred">
        <xsl:with-param name="nm">$name</xsl:with-param>

<xsl:template name="fred">
    <xsl:param name="nm"/>
and .=$nm]]" group-by=".">
        <xsl:sort select="column[@name='sort_order']" data-type="number"/>
        <xsl:value-of select="concat(column[@name='property_nm'], '=',

but I'm getting an error message that my context item:
and .=$nm]]"
isn't a node.  I've evaluated that xpath, without the variable name,
and it selects successfully...

Thanks for any hints


Current Thread