Re: [xsl] A problem using the xsl:key facility

Subject: Re: [xsl] A problem using the xsl:key facility
From: Nathan Potter <ndp@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 23 Jul 2010 13:22:59 -0400
Michael,

I had an inkling that the key issue related to some kind of scoping issue but I couldn't articulate it all. Thanks for that!

Based on your tip I did:

<xsl:key name="service-by-name" match="//thredds:service" use="@name"/>
<xsl:variable name="documentRoot" select="/"/>



And then later:


<xsl:template name="dapServices">
<xsl:param name="serviceNames"/>
<xsl:for-each select="$serviceNames/thredds:serviceName">
<xsl:apply-templates mode="dapServices" select="key('service-by-name',.,$documentRoot)"/>
</xsl:for-each>
</xsl:template>


<xsl:template mode="dapServices" match="thredds:service">
<xsl:copy-of select="descendant-or- self::thredds:service[lower-case(@serviceType)='opendap']"/>
</xsl:template>


And it all works great. Thanks for the efficiency tips too. I know that code looked pretty tortured, the key problem had reduced my brain to mush and I was thrashing around trying all kinds of pedantic things trying to isolate the problem.

Thanks again,


Nathan



On Jul 20, 2010, at 4:30 PM, Michael Kay wrote:


On 20/07/2010 21:17, Nathan Potter wrote:

The key() function, when called with two arguments, always searches within the current document (more accurately, the document containing the context node). Your xsl:for-each changes the current document, so you are searching the wrong thing. In XSLT 2.0 you can use the third argument of the key() function to indicate which document you want to search.

I've included some other comments on your code in case you find them helpful.


<xsl:choose> <xsl:when test="boolean(dataset)">
Calling boolean() is redundant here: the test attribute does it anyway.
<xsl:element name="NestedDataset">
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
That's a rather longwinded way of doing

<NestedDataSet name="{@name}"><xsl:apply-templates/></NestedDataSet>

<xsl:variable name="sName"><xsl:value-of select="."/></xsl:variable>

You can almost invariably rewrite this as <xsl:variable name="sName" select="."/> (or perhaps select="string(.)"). This isn't just a stylistic issue, it's a lot more efficient to bind a variable to a node or a string than to construct a temporary tree.


Michael Kay
Saxonica


============================================================ Nathan Potter Oregon State University, COAS ndp at coas.oregonstate.edu 104 Ocean. Admin. Bldg. 541 231 3317 voice Corvallis, OR 97331-5503 541 737 2064 fax

Current Thread