如何在变量中存储xpath并将它与XSLT 1.0配合使用
问题描述:
我正尝试使用xpath中的for-each变量。但它给了我一个表达式必须评估为节点集的错误。如何在变量中存储xpath并将它与XSLT 1.0配合使用
节点名被定义为
<xsl:variable name="NodeName" select="name(.)"/>
<xsl:variable name="SyncPath"
select="concat('/combinedxml/com.csc_PolicySyncRs/',$NodeName)"/>
,这里是for-each循环
<xsl:for-each select="$SyncPath/*">
答
我会胡乱猜测和假设你有兴趣转换变量节点集以便稍后在XPath中使用它。这可以使用扩展功能exsl:node-set()
完成。 documentation有使用的例子。
引用:
此用例示出了使用exsl所述的 结果:节点集()将一个 结果树片段转换为节点集。
源
<doc>
<one />
<two />
<three />
<four />
</doc>
样式表
<!-- Test exslt:node-set applied to a result tree fragment -->
<xsl:variable name="tree">
<a>
<b>
<c>
<d />
</c>
</b>
</a>
</xsl:variable>
<xsl:template match="/">
<out>
<xsl:value-of select="count(exslt:node-set(//*))" />
</out>
</xsl:template>
结果
<out xmlns:exslt="http://exslt.org/common">5</out>
+0
即使添加了命名空间,exslt仍未定义。 – user433023 2011-04-08 14:59:42
答
XSLT 1.0(实际上2.0)具有用于构建XPath表达式作为一个字符串,然后计算它没有标准设施。许多处理器具有扩展功能要做到这一点,各种所谓的达因:评价,撒克逊:评估等
答
使用:
<xsl:variable name="SyncPath"
select="/combinedxml/com.csc_PolicySyncRs/*[name()=$NodeName]"/>
能否请您提供您指的是很好地格式化XSLT片段? – 2011-04-08 14:51:31
你能说明$ NodeName是如何定义的吗? – 2011-04-08 14:59:00
这是http://*.com/questions/4377811/using-dynamic-xpath-in-xslt或http://*.com/questions/4682151/xslvariable-as-xpath-value-for- other-xsl-tag或http://*.com/questions/1551526/is-it-possible-to-use-a-dynamic-xpath-expression-in-a-xslt-style-sheet或http: //www.google.com/search?q=site%3A*.com+xslt+dynamic+xpath – 2011-04-08 15:10:07