我得到了这个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是否会生成此结果树片段,因为我不知道如何打印它(出于调试目的) .
1 回答
尝试