[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Confusion about xsl:param and key()


Subject: RE: [xsl] Confusion about xsl:param and key()
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 28 Mar 2006 00:39:05 +0100

So long as the value will always be an element name, use

*[name()=$groupBy]

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

> -----Original Message-----
> From: Tom Rippetoe [mailto:tomrippetoe@xxxxxxx] 
> Sent: 28 March 2006 00:24
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Confusion about xsl:param and key()
> 
>  
> Hello.
>  
> I am trying to create a stylesheet that will process my data 
> (see below for
> sample) to produce an SVG bar chart of mean values of some 
> attribute. The
> data ends up being grouped to generate the classes of the bar 
> chart. For
> example, I may want to group based on <spcd>, and plot the 
> mean tree <ht>
> for each <spcd>.  I have created a stylesheet that can do 
> this, but its a
> static stylesheet - I have hardcoded in <spcd> and <ht>. I 
> was hoping to
> make the stylesheet more dynamic by adding stylesheet 
> parameters for the
> grouping variable and for the attribute of interest, but so 
> far that has
> failed. I have tried to set up a grouping parameter and 
> passing in a value
> for that parameter but the graph does not look at all like 
> the hard-coded
> version. Instead of producing bars for each <spcd> group, 
> bars are produced
> for each <tree> element.
>  
> Here is a code sample. You can see some of the many places that I have
> hardcoded in the value 'spcd'. I want to end my dependence on 
> hard coding in
> the values and use parameter values instead. How do I do 
> that? xslt version
> is 2.0 and processor is saxon 8.7.
>  
> Small sample of XML
> 
> <row>
>   <data>	 
>     <tree>
>        <vol>66.111838</vol>
>        <ht>97</ht>
>        <dia>18.80</dia>
>        <spgrpcd>10</spgrpcd>
>        <spcd>202</spcd>
>        <fortypcd>22</fortypcd>
>        <owngrpcd>12<owngrpcd>
>     </tree>
>     <tree>
>        <vol>46.201</vol>
>        <ht>67</ht>
>        <dia>8.67</dia>
>        <spgrpcd>20</spgrpcd>
>        <spcd>11</spcd>
>        <fortypcd>22</fortypcd>
>        <owngrpcd>12<owngrpcd>
>     </tree>
>   </data>
>  </row>
> 
> <xsl:stylesheet version="2.0" 
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>  xmlns:exsl_common="http://exslt.org/common"
>  xmlns:exsl_math="http://exslt.org/math"
>  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>  xmlns:saxon="http://saxon.sf.net/"
>  extension-element-prefixes="exsl_math exsl_common">
>  
>  <xsl:output omit-xml-declaration="no" standalone="yes" indent="yes"
> method="xml"/>
>  
>  <xsl:param name="groupBy" select="'spcd'" as="xs:string" />
>  
>  <!-- kept this 'spcd' hardcoded but want to 'parameterize' 
> it as well -->
>  <xsl:key name="speciesCodes" match="//row/data/tree" use="spcd" />
>  <xsl:variable name="width" select="1000" />
>  <xsl:variable name="height" select="1000" />
>  
>  <xsl:variable name="pathToTrees" select="//row/data/tree" />
>  
>  <xsl:template match="/">
>   <svg xmlns:svg="http://www.w3.org/2000/svg"
>    xmlns:xlink="http://www.w3.org/1999/xlink"
>    xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
>    onload="init(evt)"
>    viewBox="0 0 {$width} {$height}">
>     <g id="container">
>      <!-- Draw outline of graph -->
>      <rect x="0" y="0" width="{$width}" height="{$height}"
> style="stroke:#a0a0a0;fill:white;stroke-			
> width:0.5"
> onclick="addRect(evt)"></rect>
> 
>      	<!-- This is here for testing purposes but demonstrates 
> the problem.
> If I use the literal 'spcd', 		I get a list of the seven unique
> <spcd> in my data. If I use the variable $groupBy, I get
>       	a list of all the individual trees -->     
>  
>     	<!-- This produces a list of seven unique <spcd>'s
> 	<xsl:for-each select="$pathToTrees[count(. | key('speciesCodes',
> spcd)[1]) = 1]">
>           <text x="200" y="{11 * position()}"><xsl:value-of 
> select="spcd"
> /></text>
>      	</xsl:for-each>
>     
> 	<!-- This produces a list of text values 'spcd'; one 
> for each tree -
> all 500 of them
> 	<xsl:for-each select="$pathToTrees[count(. | key('speciesCodes',
> $groupBy)[1]) = 1]">
>           <text x="200" y="{11 * position()}"><xsl:value-of
> select="$groupBy" /></text>
>      	</xsl:for-each>
>     </g>
>   </svg>
>  </xsl:template>
> </xsl:stylesheet>
>  
> Thank you for any help.


Current Thread
Keywords