David Marston wrote:
> Could you please show us how you added that for-each? I tried
> to make a verson of Uche's example and couldn't get it to work
> without a for-each encompassing the call to key(), which I think
> is required by the spec.
Here is a working version of my code. It illustrates two solutions
I found: one was to use the foreach, and the other was to use the
key expression within a node test in a location path starting with
document(...). The latter version is probably less efficient and
makes it pointless using key at all.
The input document is irrelevant and the file java_reserved_words.xsl
has this format:
<?xml version = '1.0' encoding = 'UTF-8' ?>
<reserved_words>
<reserved_word name="abstract" language="Java"/>
<!-- other reserved words omitted for now -->
</reserved_words>
Here's the stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="java_reserved_word"
match="//reserved_word[@language='Java']"
use="@name" />
<xsl:template match="/">
<xsl:call-template name="identifier_mapping">
<xsl:with-param name="identifier" select="'abstract'"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="identifier_mapping2">
<xsl:with-param name="identifier" select="'abstract'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="identifier_mapping">
<xsl:param name="identifier"/>
<!-- Use for-each to set context to java_reserved_words.xml tree
so that we can refer to keys in that document -->
<xsl:for-each select="document('java_reserved_words.xml')">
<xsl:if test="key('java_reserved_word',$identifier)">
<xsl:text>_</xsl:text>
</xsl:if>
<xsl:value-of select="$identifier"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="identifier_mapping2">
<xsl:param name="identifier"/>
<xsl:if
test="document('java_reserved_words.xml')/reserved_words/reserved_word[.=key
('java_reserved_word',$identifier)]">
<xsl:text>_</xsl:text>
</xsl:if>
<xsl:value-of select="$identifier"/>
</xsl:template>
</xsl:stylesheet>
- Stephen