xml 打印出 XSL 变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/709594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing Out The Value Of An XSL Variable
提问by Lee Theobald
Can someone please tell me how to print out a variable in my XSL transform? Seems like an easy enough thing to do but I just can't seem to do it. Here's the code I have:
有人能告诉我如何在我的 XSL 转换中打印出一个变量吗?似乎是一件很容易做的事情,但我似乎无法做到。这是我的代码:
<?xml version='1.0' encoding='UTF-8' ?>
<xsl:stylesheet version="1.0"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="ControlledListStructure">
<xsl:param name="xmlElem" />
<xsl:param name="dataName" />
<xsl:element name="{$xmlElem}">
1: <xsl:text>{$xmlElem}</xsl:text>.
2: {$xmlElem}.
</xsl:element>
</xsl:template>
</xsl:stylesheet>
If I called this template with a value for xmlElem of "Wibble" (a string - not a node), I would get the following output:
如果我使用“Wibble”的 xmlElem 值(字符串 - 不是节点)调用此模板,我将得到以下输出:
<Wibble>
1: {$xmlElem}.
2: {$xmlElem}.
</Wibble>
So my parameter is coming over properly, I just can't access it properly. Can someone tell me how I can get $xmlElem to print out properly so that I see:
所以我的参数正确地过来了,我只是无法正确访问它。有人能告诉我如何让 $xmlElem 正确打印出来,以便我看到:
<Wibble>
1: Wibble.
2: Wibble.
</Wibble>
Thanks for any input.
感谢您提供任何意见。
回答by Dimitre Novatchev
All answers are missing something important: read further:
所有的答案都缺少一些重要的东西:进一步阅读:
Can someone please tell me how to print out a variable in my XSL transform? Seems like an easy enough thing to do but I just can't seem to do it.
有人能告诉我如何在我的 XSL 转换中打印出一个变量吗?似乎是一件很容易做的事情,但我似乎无法做到。
In XSLT 1.0 there are two main ways of producing the contents of an <xsl:variable>,depending on whether it contains a scalar value (string, number or boolean), or has a structured value -- a node-set (one or more nodes from xml document(s) ):
在 XSLT 1.0 中,有两种主要的方式来生成 的内容<xsl:variable>,这取决于它是否包含标量值(字符串、数字或布尔值),或者是否具有结构化值——节点集(来自 xml 文档的一个或多个节点) (s) ):
<xsl:value-of select="$yourscalarVariableName"/>Use this to produce a scalar value. Actually produces a text node, containing this scalar value.<xsl:copy-of select="$yourStructuredVariableName"/>Use this to produce a copy of all nodes contained in the variable.
<xsl:value-of select="$yourscalarVariableName"/>使用它来产生一个标量值。实际上产生一个文本节点,包含这个标量值。<xsl:copy-of select="$yourStructuredVariableName"/>使用它来生成变量中包含的所有节点的副本。
It is very important to knowthat if an xsl:variablecontains a list of nodes and the <xsl:value-of ...>instruction is used, only the string value of the first node will be produced. This is a frequently committed error and a FAQ.
要知道,如果 anxsl:variable包含节点列表并且使用该<xsl:value-of ...>指令,则只会产生第一个节点的字符串值,这一点非常重要。这是一个经常犯的错误和常见问题解答。
There is a third way: if the <xsl:variable>should be used in producing an attribute:
还有第三种方法:如果<xsl:variable>应该在生成属性时使用:
<someLiteralResultElement someAttribute="{$theVariable}"/>
The XPath expression in the curly braces (called AVT -- attribute-value-template) is evaluated and the result is put into the attribute value.
对花括号中的 XPath 表达式(称为AVT -- attribute-value-template)进行评估,并将结果放入属性值中。
In XSLT 2.0, the <xsl:value-of .../>instruction , when run not in compatibility mode, produces a list of text nodes -- one for each node contained in the xsl:variable. When run in compatibility mode (has the attribute version="1.0"specified), the <xsl:value-of>instruction behaves in the same way as it does in XSLT 1.0.
在 XSLT 2.0 中,该<xsl:value-of .../>指令在非兼容模式下运行时,会生成一个文本节点列表 - xsl:variable. 在兼容模式下运行时(version="1.0"指定了属性),该<xsl:value-of>指令的行为方式与它在 XSLT 1.0 中的行为方式相同。
In Xslt 2.0<xsl:copy-of>behaves in the same way as in XSLT 1.0. However it is recommended to use the new <xsl:sequence>instruction, because the former produces a new copy of every node, while <xsl:sequence>does not produce new copies of nodes.
在 Xslt 2.0 中的<xsl:copy-of>行为方式与在 XSLT 1.0 中的行为方式相同。但是推荐使用new<xsl:sequence>指令,因为前者为每个节点产生一个新的副本,而<xsl:sequence>不会产生节点的新副本。
回答by Andrew Hare
Try this:
尝试这个:
<xsl:value-of select="$xmlElem"/>
回答by Razzie
You can use:
您可以使用:
<xsl:value-of select="$xmlElem" />
回答by David M
The {$xmlElem}syntax only works within an attribute. You need:
该{$xmlElem}语法仅适用于属性。你需要:
<xsl:value-of select="$xslElem" />

