xsl:variable 作为其他 xsl 标记的 xpath 值
我遇到了 xsl:variable
问题。我想创建一个变量,其值取决于另一个 XML 节点属性的值。这工作很好。但是,当我尝试创建一个具有表示 XPath 的字符串值的变量时,当我尝试在以后的 XSL 标记中将其用作 XPath 时,它就不起作用了。
<xsl:variable name="test">
<xsl:choose>
<xsl:when test="node/@attribute=0">string/represent/xpath/1</xsl:when>
<xsl:otherwise>string/represent/xpath/2</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:for-each select="$test">
[...]
</xsl:for-each>
我试过: 如何在 xsl if 中使用 xsl 变量 和 使用 xsl:variable 进行 xsl:for-each 选择时出现问题。但没有结果。
I'm having issues with xsl:variable
. I want to create a variable with a value that depends on the value of another XML node attribute. This working good. But when I try to create a variable with a string value that represents XPath, it just doesn't work when I try to use it as XPath in a later XSL tag.
<xsl:variable name="test">
<xsl:choose>
<xsl:when test="node/@attribute=0">string/represent/xpath/1</xsl:when>
<xsl:otherwise>string/represent/xpath/2</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:for-each select="$test">
[...]
</xsl:for-each>
I tried:
How to use xsl variable in xsl if
and
trouble with xsl:for-each selection using xsl:variable. But with no results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然而,XSLT(1.0 和 2.0)通常不支持 XPath 表达式的动态求值:
如果我们仅将每个位置路径限制为元素名称,我们就可以实现相当通用的动态 XPath 求值器:
当此转换应用于此 XML 文档时:
生成所需的正确结果:
Dynamic evaluation of an XPath expression is generally not supported in XSLT (both 1.0 and 2.0), however:
We can implement a rather general dynamic XPath evaluator if we only restrict each location path to be an element name:
when this transformation is applied on this XML document:
the wanted, correct result is produced:
如果像这种情况一样提前知道这些路径,那么您可以使用:
If those path are known in advance like this case, then you can use:
这在 XSLT 1.0 中本身是不可能的,但您可以使用扩展库,例如 dyn:
http://www.exslt.org/dyn/functions/evaluate/dyn.evaluate.html
dyn:evaluate
函数将字符串计算为 XPath 表达式。This is not natively possible in XSLT 1.0, but you can use an extension library such as dyn:
http://www.exslt.org/dyn/functions/evaluate/dyn.evaluate.html
The
dyn:evaluate
function evaluates a string as an XPath expression.