문제

I feel like I'm missing something obvious, but I can't work out why my XSLT 1.0 key is not working for me.

My desired output is "Sample Brand" (see comment in XSLT below), but nothing is output at all.

The testing I've done seems to indicate that the key isn't being generated, as when I do a for-each using the key() function with some dummy output, nothing is output then either (it seems like there are 0 key items). But I'm not sure of this.

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>
도움이 되었습니까?

해결책

It's now been pointed out to me that I neglected to put the key name in quotes:

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

Key now working as expected.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top