RE: [xsl] How to iterate over the output of distinct-values()?

Subject: RE: [xsl] How to iterate over the output of distinct-values()?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 6 Jun 2006 18:09:17 +0100
Within the execution of

<xsl:for-each select="distinct-values(...)">

the context item is an atomic value.

When you do count(/ROWSET/ROW[CONTRACTOR='$contractor']), the meaning of the
initial "/" is "find the root of the tree containing the context node". But
there isn't a context node (the context item is an atomic value). You need
to pass the template a parameter telling it what document to look in, and
use an expression such as count($input/ROWSET/ROW[CONTRACTOR=$contractor]).

The reason you're getting 0 is that '$contractor' shouldn't be in quotes.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: cknell@xxxxxxxxxx [mailto:cknell@xxxxxxxxxx] 
> Sent: 06 June 2006 17:56
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] How to iterate over the output of distinct-values()?
> 
> I have a document that consists of elements like this.
> 
>  <ROW>
>   <CONTRACTOR></CONTRACTOR>
>   <CASE_NO></CASE_NO>
>   <SCHD_DATE></SCHD_DATE>
>   <VERBAL></VERBAL>
>   <COMPLT></COMPLT>
>   <DOE_DTE></DOE_DTE>
>  </ROW>
> 
> While there are a few thousand rows, there are only a few 
> distinct values for CONTRACTOR. I am working on a stylesheet 
> that will perform various counts of data in these elements.
> 
> I thought I would get the distinct values for CONTRACTOR with 
> the distinct-values() function and the supply each distinct 
> value as a parameter to an xsl:call-template, like this:
> 
> <xsl:for-each select="distinct-values(ROWSET/ROW/CONTRACTOR)">
>   <xsl:sort select="." />
>   <xsl:call-template name="contractor-row">
>      <xsl:with-param name="contractor" select="." />
>   </xsl:call-template>
> </xsl:for-each>
> 
> The context node where this is done is "/" and ROWSET in the 
> root element.
> 
> When the xsl:call-template is executed, Saxon gives this error:
> 
> "XPTY0020: Finding root of tree: the context item is not a node"
> 
> Here is the relevant part of the called template:
> 
> <xsl:template name="contractor-row">
>   <xsl:param name="contractor" />
>   <tr height="17" style='height:12.75pt'>
>     <td height="17" class="xl28" 
> style='height:12.75pt;border-top:none'>
>       <xsl:value-of select="$contractor" />
>     </td>
>     <td class="xl26" align="right" 
> style='border-top:none;border-left:none' x:num="">
>       <xsl:value-of 
> select="count(/ROWSET/ROW[CONTRACTOR='$contractor'])" />
>     </td>
>  ... more template here ...
> </xsl:template>
> 
> The processor emits this error message when evaluating
> count(/ROWSET/ROW[CONTRACTOR='$contractor'])
> 
> Now if I change the argument to the count() function to read:
> count(document('path-to-xml-file')/ROWSET/ROW[CONTRACTOR='$con
> tractor'])
> 
> The error goes away. It also returns a value of '0' (zero) 
> for every call of the function.
> 
> Can anyone shed light on any part of this (to me, at least) puzzle?
> 
> Thanks.
> 
> --
> Charles Knell
> cknell@xxxxxxxxxx - email

Current Thread