我正在尝试使用 XSLT 变量,但没有取得太大成功,希望我只是做了一些愚蠢的事情。
我有以下代码片段:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xslt"
version="1.0">
<xsl:template match="/">
<xsl:variable name="config" select="query/@config"></xsl:variable>
所以我希望有一个变量“config”设置为我的顶级元素“query”的“config”属性的值。
然后,我稍后尝试在样式表中使用该变量,例如:
<a href="localhost/test?go">
{$config}
</a>
但是我在输出 HTML 文档中看到的输出是:
<a href="localhost/test?go">
{$config}
</a>
所以该值没有像我预期的那样被替换。
我认为这几乎是最简单的情况,所以我正在做一些愚蠢的事情! 请帮忙,谢谢!
更新感谢所有回复的人,我误解了我是在属性中还是在外部工作的不同上下文。 很好地解决了我的问题!
如果可以的话,我会接受两个答案,一个是我的,另一个是@Aaron Digulla,它解释了属性的事情。
I am trying to use XSLT variables and not having much success, hopefully I'm just doing something dumb.
I have the following code snippet:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xslt"
version="1.0">
<xsl:template match="/">
<xsl:variable name="config" select="query/@config"></xsl:variable>
so I expect there to be a variable 'config' set to the value of the 'config' attribute of my top-level element 'query'.
I then try to use the variable later in my stylesheet, for example:
<a href="localhost/test?go">
{$config}
</a>
but the output I see in my output HTML document is:
<a href="localhost/test?go">
{$config}
</a>
so the value has not been substituted as I would have expected.
I think this is pretty much the simplest case there could be, so I'm doing domething stupid! Please help, thanks!
UPDATE thanks to all who responded, I misunderstood the different contexts of whether I was working in an attribute or outside. Sorted my problem out nicely!
If I could I would accept two answers, the one I have, and @Aaron Digulla's, which explained the attributes thing.
发布评论
评论(6)
这里有两个问题看似相同,但略有不同:1)如何通过名称引用变量? 2) 我可以在哪里通过名称引用变量?
首先,变量总是使用
$varname
语法引用。 其次,这可以在任何允许表达式的地方完成。 这是第二部分,这似乎令人困惑。 首先,默认情况下,元素或属性的值将按字面意思输出,因此在以下示例中实际上没有引用任何变量:输出将按字面意思匹配键入的内容。
要输出变量的值,我们需要在允许表达式的地方引用它。 在元素内容中,我们使用
xsl:value-of
; 在被视为属性值模板的属性中(例如文字结果元素),表达式由大括号{}
分隔。 假设有以下声明:...然后是以下内容:
...结果:
关于 AVT 和
value-of
的一些说明:$test< /代码>。 AVT 中的大括号不是变量引用的一部分; 它们是表达式分隔符。
xsl:value
的select
属性已经接受表达式作为内容。问题:为什么不能像这样在属性中使用
value-of
?答案:因为 XSLT 文档必须包含格式良好的 XML(但事实并非如此)。
问题:为什么不能在元素内容中使用
{$varname}
?圆滑的回答:因为 XSLT 的创建者并没有那样设计。
There are two questions here that seem the same, but which are subtly different: 1) How do I reference a variable by name? 2) Where can I reference a variable by name?
First, variables are always referenced using the
$varname
syntax. Second, this can be done anywhere an expression is allowed. It's the second part of this that seems to confuse. To start with, the value of an element or attribute will by default be output literally, so no variables are actually referenced in the following example:The output will literally match what was typed.
To output a variable's value, we need to reference it where an expression is allowed. In element content, we use
xsl:value-of
; in attributes that are treated as an Attribute Value Template (e.g. the attributes of a literal result element), expressions are delimited by curly braces{}
. Assume the following declaration:...then the following:
...results in:
A few parting notes on AVTs and
value-of
:$test
. The braces in an AVT are not part of the variable reference; they are expression delimiters.xsl:value
'sselect
attribute already accepts an expression as content.Question: Why can't you use
value-of
in an attribute, like this?Answer: Because XSLT documents must contain well-formed XML (and that isn't).
Question: Why can't you use
{$varname}
in element content?Glib answer: because XSLT's creators didn't design it that way.
在样式表中,您必须使用:
而不是:
In your stylesheet you must use:
instead of:
{$config}
仅适用于 XSLT 元素的属性。 (注意:美元在大括号内,因为完整的 XPath 表达式必须用大括号括起来){$config}
only works in attributes of XSLT elements. (Note: dollar inside the brace because the complete XPath expression must be surrounded by braces)使用
而不是{$config}
。Use
<xsl:value-of select="$config"/>
instead of{$config}
.@lwburk 的答案很好(+1),但最后一个问题/答案仅适用于 XSLT 1.0 和 2.0。 在 3.0 中,您可以使用 TVT(文本值模板;类似于 @lwburk 的回答中提到的 AVT(属性值模板)。
要使用 TVT,请将标准属性
xsl:expand-text="yes"
添加到元素。 这将导致处理器将该元素的后代文本节点视为 TVT。示例:
XSLT 3.0
输出(使用任何格式良好的 XML 作为输入)
注意:使用 Saxon-PE 9.5 进行测试。
@lwburk's answer is great (+1), but the last question/answer is only accurate for XSLT 1.0 and 2.0. In 3.0 you can use a TVT (Text Value Template; similar to the AVT (Attribute Value Template) mentioned in @lwburk's answer).
To use a TVT, add the standard attribute
xsl:expand-text="yes"
to the element. This will cause the processor to treat descendant text nodes of that element as a TVT.Example:
XSLT 3.0
Output (using any well-formed XML as input)
Note: Tested using Saxon-PE 9.5.
因为您需要使用
?Because you need to use
<xsl:value-of select="config"/>
?