您认为这会起作用吗? (看来您不能在动态创建的元素上使用 XPath,可能是因为它们不在用于使 XPath 表现良好的“幕后”魔法中)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="data">
<fu><bar>Test</bar></fu>
</xsl:variable>
Value: <xsl:value-of select="$data/fu/bar" />
</xsl:template>
</xsl:stylesheet>
我在尝试过的 XSLT 处理器中遇到错误,例如使用 PHP5 时出现“无效类型”;从“data”之前删除“$”将使错误消失,但显然不会执行所需的操作,因为它将尝试匹配运行 XSLT 的 XML 文档中的 data/fu/bar 。
最佳答案
您可以使用document()
具有空字符串值的函数,以便引用进行函数调用的样式表的根节点。然后,使用 XPath 选择您想要的内容。
因此,不要在选择中引用 $data
变量,而是使用以下 XPath:
document('')/xsl:stylesheet/xsl:template[@match='/']/xsl:variable[@name='data']/fu/bar
应用于您的 XSLT:
<xsl:template match="/">
<xsl:variable name="data">
<fu><bar>Test</bar></fu>
</xsl:variable>
Value: <xsl:value-of
select="document('')/xsl:stylesheet
/xsl:template[@match='/']/xsl:variable[@name='data']/fu/bar" />
</xsl:template>
</xsl:stylesheet>
关于php - 在 XSLT 中,您可以在动态创建的元素上使用 XPath 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6767948/