为什么我的XSLT密钥不能正常工作?

时间:2012-06-21 21:49:37

标签: xslt xslt-1.0 symphony-cms

我觉得我错过了一些明显的东西,但我无法理解为什么我的XSLT 1.0密钥对我不起作用。

我想要的输出是“Sample Brand”(请参阅​​下面的XSLT中的评论),但根本没有输出。

我已经完成的测试似乎表明没有生成密钥,就像我使用带有一些虚拟输出的for-each函数执行key()时,没有输出任何内容(似乎有0个关键项目)。但我不确定这一点。

XML:

<data>
    <products-by-instances>
        <entry id="1975">
            <name>Sample Name</name>
            <brand>
                <item id="1970">Sample Brand</item>
            </brand>
            <instances>
                <item id="1972">MILT501</item>
                <item id="1974">MILT502</item>
            </instances>
        </entry>
    </products-by-instances>
    <shopping-cart items="2" total="35">
        <item id="1972" num="1" sum="5" />
        <item id="1974" num="3" sum="30" />
    </shopping-cart>
</data>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="products-by-instance-id" match="/data/products-by-instances/entry" use="instances/item/@id"/>

<!-- other templates redacted for brevity; the below template is being applied -->

<xsl:template match="/data/shopping-cart/item">
    <xsl:value-of select="key(products-by-instance-id, @id)/brand/item"/>
    <!-- desired output is "Sample Brand" -->
</xsl:template>

1 个答案:

答案 0 :(得分:2)

现在我已经向我指出,我忽略了将关键名称放在引号中:

<xsl:value-of select="key('products-by-instance-id', @id)/brand/item"/>

Key现在按预期工作。

相关问题