我遇到了一个预期的问题,我想知道是否有比我更好的解决方案。
所以,我有一个文档节点变量:
<xsl:variable name="variableNode" as="document-node()">
<xsl:document>
<root>
<element>...content...</element>
<root>
</xsl:document>
</xsl:variable>
此文档节点变量的内容稍后由for-each循环处理:
<xsl:for-each select="$variableNode/root/element">
...content...
</xsl:for-each>
从这个for-each循环的内容中,我试图通过这个xslt样式表获取实际xml文件的元素。但那并没有起作用。
所以,我把文件名放到像这样的变量
中<xsl:variable name="actual_filename" select="base-uri()"/>
并通过doc-Function获得所需的元素。 (doc($ actual_filename)/)
这在我的情况下效果很好,但我想知道它是否是一个优雅的解决方案,特别是如果输入不是文件而是其他一些xml内容。
你能想到更好的解决方案吗?谢谢!
答案 0 :(得分:3)
通常的方法是顶级
<xsl:variable name="main-root" select="/"/>
将主输入文档节点存储在变量中,然后在for-each或模板中使用来自不同文档的上下文节点,您始终可以访问变量
<xsl:for-each select="$variableNode/root/element">
<xsl:value-of select="$main-root//foo[@id = current()/@id]"/>
</xsl:for-each>