XSLT 1.0 -提取节点集并作为参数传递



我得到了这个XML,并且必须从它中渲染相当多的内容,并且大多数工作正常,但我试图提取color的节点集,其键与bar元素的key相匹配,属性是硬编码字符串(在这种情况下为'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是否甚至会生成这个结果树片段,因为我不知道如何打印它(用于调试目的)。

Try

<xsl:with-param name="colors" select="key('colorlookup', bar[generate-id() = generate-id(key('barnode', @key)[1])]/@key)[@name = 'data']"/>

最新更新