【问题标题】:XSLT 1.0 How to use xsl:key with document() functionXSLT 1.0 如何使用 xsl:key 和 document() 函数
【发布时间】:2013-02-12 20:18:58
【问题描述】:

我正在尝试使用 xsl:key 通过 XSL document() 函数在外部 XML 文档中查找项目。如果我不使用 document(),而是合并两个 XML 文件(在 C# 中使用 XmlDocument),我可以让 xsl:key 部分工作。但是,这两个 XML 文件都非常大,在某些情况下我开始出现“内存不足”错误。我还需要能够使用 xls:key,否则这个过程需要几个小时。

在 XSLT 2.0 中,我相信您可以这样做:

<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>

您如何在 XSLT 1.0 中实现这一点?

【问题讨论】:

    标签: document xslt-1.0 xslkey


    【解决方案1】:

    你有两种可能:

    无钥匙

    <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>
    

    带钥匙

    key定义适用于所有文档,但在使用key()函数之前需要更改上下文节点:

    <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>
    

    另请参阅 Mike KayJeni Tennison 关于此主题的两个很棒的邮件列表答案

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      相关资源
      最近更新 更多