如何将变量传递给 XSLT XPath 表达式? [英] How do I pass a variable to XSLT XPath expression?
问题描述
如何将变量传递给 XSLT XPath.例如
How can I pass a variable to XSLT XPath. For example
<ns0:UnitCode>
<xsl:value-of select="//ns1:Root/InputMessagePart_1/
ns2:ServiceMasterDetailsResponse/
ns2:ServiceMasterDetailResponse[$Counter]/
ns2:BASE_UOM/text()"/>
</ns0:UnitCode>
其中 Counter
是我的 XSLT 变量.
where Counter
is my XSLT variable.
我想将变量 counter 的数值传递给上面的 XSLT XPath 表达式.
I want to pass the numeric value of the variable counter to the above XSLT XPath expression.
推荐答案
如何在 XSLT 中使用变量引用
XPath
在 select
属性中,使用 $var
(正如您所做的那样):
Within a select
attribute, use $var
(as you've done):
<xsl:value-of select="/path/to/element[@id=$var]"/>
以下是在 XSLT 中使用变量引用的一些其他方式...
Here are some other ways variable references can be used in XSLT...
匹配模式
XSLT 1.0:变量不是match
中的 em> 允许:
XSLT 1.0: Variables are not allowed in match
:
匹配属性的值包含一个错误VariableReference
.
It is an error for the value of the match attribute to contain a
VariableReference
.
XSLT 2.0: Variables are allowed in match
predicates or
模式可以以 id FO 或键函数调用开始,前提是要匹配的值作为文字提供或对变量或参数的引用,以及键名(在key 函数的情况)作为字符串文字提供.这些模式永远不会匹配树中根不是 a 的节点文档节点.
Patterns may start with an id FO or key function call, provided that the value to be matched is supplied as either a literal or a reference to a variable or parameter, and the key name (in the case of the key function) is supplied as a string literal. These patterns will never match a node in a tree whose root is not a document node.
XSLT 2.0 匹配谓词变量示例:
XSLT 2.0 Match Predicate Variable Example:
<xsl:variable name="globalVar" select="'value'"/>
<xsl:template match="abc[. = $globalVar]">
文字结果元素
<img src="{$var}/image.jpg"/>
元素或属性名称
在元素或属性名称中,使用 xsl:element
或 xsl:attribute
和属性值模板:
Within an element or attribute name, use xsl:element
or xsl:attribute
and an attribute value template:
<xsl:element name="{$var}">content</xsl:element>
或
<xsl:attribute name="{$var}">content</xsl:attribute>
这篇关于如何将变量传递给 XSLT XPath 表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!