Group dynamically generated nodeset

283 views Asked by At

I am having trouble dynamically grouping with XSLT.

My problem is complicated (I think) in part because the nodeset I'm trying to transform is dynamically generated from the apache xalan/sql extension. I closely followed the example on the documentation site: see link.

The XSLT project is large; I've included only the essentials below:

<xsl:template match="/">
    <!-- I omitted the connection management code -->
    <xsl:variable name="result" select="sql:query($connection, $query)"/>
    <xsl:apply-templates select="$result/sql/row-set"/>
</xsl:template>

<xsl:template match="row">
    <xsl:apply-templates select="col"/>
</xsl:template>

<xsl:template match="col">
  <xsl:value-of select="text()"/>
</xsl:template>

The $result nodeset has this structure:

<sql>
    <row-set>
        <row>
            <col column-label='Name'>Bob</col>
            <col column-label='Site'>Site1</col>
            <col column-label='Active'>Yes</col>
        </row>
        <row>
            <col column-label='Name'>Sally</col>
            <col column-label='Site'>Site2</col>
            <col column-label='Active'>Yes</col>
        </row>
        <row>
            <col column-label='Name'>Sam</col>
            <col column-label='Site'>Site1</col>
            <col column-label='Active'>No</col>
        </row>
        <row>
            <col column-label='Name'>Jeff</col>
            <col column-label='Site'>Site2</col>
            <col column-label='Active'>Yes</col>
        </row>
    </row-set>   
</sql>

Desired result:

<sql>
    <row-set>
        <site>
            <site-name>Site1</site-name>
            <active>
                <name>Bob</name>                        
            </active>
            <in-active>
                <name>Sam</name>
            </in-active>
        </site>
        <site>
            <site-name>Site2</site-name>
            <active>
                <name>Sally</name>
                <name>Jeff</name>                        
            </active>
            <in-active/>
        </site>
    </row-set>   
</sql>

I've had a difficult time trying to group using keys a la muenchian since match doesn't allow variables:

<xsl:key name="group-site" match="$result/sql/row-set/" use=?>
1

There are 1 answers

0
michael.hor257k On

I've had a difficult time trying to group using keys a la muenchian since match doesn't allow variables:

You do not need to use a variable in the match definition. Define your key element as:

<xsl:key name="row-by-site" match="row" use="col[@column-label='Site']"/>

You must be in the context of the "document" created by the query when calling the key() function - otherwise it will look for the rows to match in the source XML document.