我想知道是否有办法循环遍历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
发布评论