[英]XSLT 1.0 - extract node-set and pass as param
我已經獲得了這個XML,並且必須從中渲染很多,並且大多數都可以正常工作,但是我試圖提取color
的節點集(其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是否會生成此結果樹片段,因為我不知道如何打印(出於調試目的)。
嘗試
<xsl:with-param name="colors" select="key('colorlookup', bar[generate-id() = generate-id(key('barnode', @key)[1])]/@key)[@name = 'data']"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.