XSLT 1.0 How to use xsl: key with document () function

I am trying to use xsl: key to search for elements in an external XML document using the XSL document () function. I can get the xsl: key key part if instead of using document (), I just merge the two XML files (using XmlDocument in C #). However, both XML files are very large, and in some cases I start getting errors from memory. I also need to be able to use xls: key, otherwise the process takes several hours.

In XSLT 2.0, I believe you can do something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="lookupDoc" select="document('CodeDescriptions.xml')" /> <xsl:key name="LookupDescriptionByCode" match="Code/@description" use="../@code" /> <xsl:template match="ItemCode"> <xsl:call-template name="MakeSpanForCode"> <xsl:with-param name="code" select="text()" /> </xsl:call-template> </xsl:template> <xsl:template name="MakeSpanForCode"> <xsl:param name="code" /> <xsl:element name="span"> <xsl:attribute name="title"> <xsl:value-of select="$lookupDoc/key('LookupDescriptionByCode', $code)" /> </xsl:attribute> <xsl:value-of select="$code" /> </xsl:element> </xsl:template> </xsl:stylesheet> 

How do you do this in XSLT 1.0?

+4
source share
1 answer

You have two options:

without a key

 <xsl:template name="MakeSpanForCode"> <xsl:param name="code" /> <xsl:element name="span"> <xsl:attribute name="title"> <xsl:value-of select="$lookupDoc/*/Code[@code = $code]/@description" /> </xsl:attribute> <xsl:value-of select="$code" /> </xsl:element> </xsl:template> 

with a key

The key definition applies to all documents, but before using the key () function, you need to change the node context:

 <xsl:template name="MakeSpanForCode"> <xsl:param name="code" /> <xsl:element name="span"> <xsl:attribute name="title"> <!-- trick: change context node to external document --> <xsl:for-each select="$lookupDoc"> <xsl:value-of select="key('LookupDescriptionByCode', $code)"/> </xsl:for-each> </xsl:attribute> <xsl:value-of select="$code" /> </xsl:element> </xsl:template> 

Also see two great mailing lists from Mike Kay and Janie Tennyson on this subject.

+9
source

All Articles