我想知道是否有方法循環遍歷XSL中指定的值,而不是來自XML。循環遍歷XSL樣式表中指定的硬編碼值,而不是XML
比方說,我有3個可能的複選框,來自XML的「當前」值。我有一個XML文檔一樣
<rootNode>
<val>bar</val>
</rootNode>
和XSL代碼像
<input id="foo" type="checkbox" name="myvar" value="foo">
<xsl:if test="val='foo'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
</input> <label for="foo">foo</label>
<input id="bar" type="checkbox" name="myvar" value="bar">
<xsl:if test="val='bar'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
</input> <label for="bar">bar</label>
<input id="baz" type="checkbox" name="myvar" value="baz">
<xsl:if test="val='baz'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
</input> <label for="baz">baz</label>
這工作,但XSL是非常詳細。我希望能夠做這樣的事情:
<!-- this syntax doesn't work, is there something similar that does? -->
<xsl:variable name="boxNames" select="'foo','bar','baz'"/>
<xsl:for-each select="name in $boxNames">
<input id="{$name}" type="checkbox" name="myvar" value="{$name}">
<xsl:if test="val=$name">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
</input> <label for="{$name}"><xsl:value-of select="$name"/></label>
</xsl:for-each>
我可以種通過將在模板的代碼,並使用多個<call-template>
<with-param>
電話得到這個,但是這並沒有保存過多少空間原版的。
有沒有簡潔的方式來做到這一點與XSL?我絕對不能把所有的複選框名稱放在XML輸出中,這是一個很大的列表,並且不必要地膨脹了XML。
除了將數據放入未使用的模板中,它可以作爲頂級元素放置,只要它使用的不是XSLT名稱空間的其他名稱空間。 XSLT處理器將忽略這樣的頂級元素。 –
完美,這正是我所希望的!我同意Jukka,我將使用一個單獨的命名空間來強化它實際上不是模板。 –