循环使用XSL样式表中指定的硬编码值,而不是XML(Looping over hard

编程入门 行业动态 更新时间:2024-10-28 04:27:14
循环使用XSL样式表中指定的硬编码值,而不是XML(Looping over hard-coded values in specified in XSL stylesheet, not in 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膨胀。

I'm wondering if there's a way to loop over a values specified in the XSL, rather than coming from the XML.

Let's say I have 3 possible checkboxes, with a "current" value that comes from the XML. I'd have an XML doc like

<rootNode> <val>bar</val> </rootNode>

and XSL code like

<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>

This works, but the XSL is very verbose. I'd LIKE to be able to do something like this:

<!-- 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>

I can KIND OF get this by putting the code in a template and using multiple <call-template> <with-param> calls, but that's doesn't save much space over the original.

Is there any concise way to do this with XSL? I definitely can't put all the checkbox names in the XML output, it's a large list and unnecessarily bloats the XML.

最满意答案

是的,您可以通过调用document('')函数来获取XSL的源(!),然后您可以将其用作节点数据源。

<xsl:template name="boxNames"> <!-- not used as template --> <name>foo</name> <name>bar</name> <name>baz</name> </xsl:template>

[...]

<xsl:variable name="boxNames" select="document('')/xsl:stylesheet/xsl:template[@name='boxNames']/name" />

Yes, you can get the source (!) of the XSL by calling the document('') function, which you can then use as node datasource.

<xsl:template name="boxNames"> <!-- not used as template --> <name>foo</name> <name>bar</name> <name>baz</name> </xsl:template>

[...]

<xsl:variable name="boxNames" select="document('')/xsl:stylesheet/xsl:template[@name='boxNames']/name" />

更多推荐

XSL,XML,label>,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-07-15 05:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   样式表   XSL   hard   Looping

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!