Igo*_*lla 16 xslt xpath dynamic
我有问题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>
Run Code Online (Sandbox Code Playgroud)
我试过: 如何在xsl中使用xsl变量ifs 和 xsl:for-trouble使用xsl:variable进行选择.但没有结果.
Dim*_*hev 13
XSLT(1.0和2.0)通常不支持动态评估XPath表达式,但是:
如果我们只将每个位置路径限制为元素名称,我们可以实现一个相当通用的动态XPath求值程序:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="inputId" select="'param/yyy/value'"/>
<xsl:variable name="vXpathExpression"
select="concat('root/meta/url_params/', $inputId)"/>
<xsl:template match="/">
<xsl:value-of select="$vXpathExpression"/>: <xsl:text/>
<xsl:call-template name="getNodeValue">
<xsl:with-param name="pExpression"
select="$vXpathExpression"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="getNodeValue">
<xsl:param name="pExpression"/>
<xsl:param name="pCurrentNode" select="."/>
<xsl:choose>
<xsl:when test="not(contains($pExpression, '/'))">
<xsl:value-of select="$pCurrentNode/*[name()=$pExpression]"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="getNodeValue">
<xsl:with-param name="pExpression"
select="substring-after($pExpression, '/')"/>
<xsl:with-param name="pCurrentNode" select=
"$pCurrentNode/*[name()=substring-before($pExpression, '/')]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于此XML文档时:
<root>
<meta>
<url_params>
<param>
<xxx>
<value>5</value>
</xxx>
</param>
<param>
<yyy>
<value>8</value>
</yyy>
</param>
</url_params>
</meta>
</root>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
root/meta/url_params/param/yyy/value: 8
Run Code Online (Sandbox Code Playgroud)
小智 12
如果这些路径是预先知道的,那么你可以使用:
<xsl:variable name="vCondition" select="node/@attribute = 0"/>
<xsl:variable name="test" select="actual/path[$vCondition] |
other/actual/path[not($vCondition)]"/>
Run Code Online (Sandbox Code Playgroud)