0

我得到了这个 XML 并且必须从中渲染相当多的内容并且大多数工作正常,但是我试图提取colorwhos 键的节点集与元素的匹配key并且bar属性是硬编码的字符串('data'在这种情况下)。节点集将作为参数传递给模板,并且每条颜色线只能出现一次:

<report>
    <settings>
        <colors>
            <color key="1-1" name="frame" value="..." ... />
            <color key="1-1" name="data" value="..." ... />
            <color key="2-1" name="frame" value="..." ... />
            <color key="2-1" name="data" value="..." ... />
            <color key="3-1" name="frame" value="..." ... />
            <color key="3-1" name="data" value="..." ... />
        </colors>
        <comp>
             <cont>
                  <bar key="1-1" .../>
                  <bar key="1-1" .../>
                  <bar key="2-1" .../>
             </cont>
        <comp>
        <!-- possibly more <comp/cont/bar> below that may not be mixed with the above -->
     </settings>
</report>

在我的 XSLT 文件中,我有这个(摘录):

<xsl:key name="barnode" match="bar" use="@key"/>
<xsl:key name="colorlookup" match="/report/settings/colors/color" use="@key"/>

<!-- this runs at the `cont` element level, i.e. `bar` can be accessed without prefix -->

<!-- set $x to the node-list of bars with unique @key attribute -->
<xsl:call-template name="renderit">
    <xsl:with-param name="colors">
        <!-- 'bars' contains node-set of 'bar' elements with @key being unique -->
        <xsl:variable name="bars" select="bar[generate-id() = generate-id(key('barnode', @key)[1])]"/>
        <xsl:for-each select="$bars">
            <xsl:value-of select="key('colorlookup', @key)[@name='data']"/>
        </xsl:for-each>
    </xsl:with-param>
</xsl:call-template>

问题是,这不会传递节点集,而是传递树片段。是否可以进行与上述相同但返回节点集的选择?

编辑:

预期的节点集:

<color key="1-1" name="data" value="..." ... />
<color key="2-1" name="data" value="..." ... />

我不确定提供的 XSLT 是否会生成这个结果树片段,因为我不知道如何打印它(出于调试目的)。

4

1 回答 1

2

尝试

<xsl:with-param name="colors" select="key('colorlookup', bar[generate-id() = generate-id(key('barnode', @key)[1])]/@key)[@name = 'data']"/>
于 2014-07-14T11:23:26.847 回答