Xpath:从每个循环的文档节点变量中选择节点 [英] Xpath: Select node from within document node variable for each loop

查看:32
本文介绍了Xpath:从每个循环的文档节点变量中选择节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个预期的问题,我想知道是否有比我更好的解决方案.

I came across an expected problem and I am wondering whether there's a solution better than mine.

所以,我有一个文档节点变量:

So, I have a document node variable:

   <xsl:variable name="variableNode" as="document-node()">
        <xsl:document>
            <root>
             <element>...content...</element>
        <root>  
        </xsl:document>    
    </xsl:variable>

此文档节点变量的内容稍后由 for-each 循环处理:

The content of this document node variable is processed later by a for-each loop:

<xsl:for-each select="$variableNode/root/element">
...content...
</xsl:for-each>

从这个 for-each 循环的内容中,我试图通过这个 xslt 样式表获取实际 xml 文件处理过程的元素.但这没有用.

From within the content of this for-each loop I tried to get elements of the actual xml file beeing processes by this xslt stylesheet. But that didnt' work.

所以,我将文件名放入这样的变量中

So, I put the filename into an variable like this

<xsl:variable name="actual_filename" select="base-uri()"/>

并通过 doc-Function 获得所需的元素.( doc($actual_filename)/)

and got the desired elements by the doc-Function. ( doc($actual_filename)/ )

这在我的情况下效果很好,但我想知道这是否是一个优雅的解决方案,特别是如果输入不是文件而是其他一些 xml 内容.

This works well in my case, but I'm wondering whether it is an elegant solution, especially if the input is no file but some other xml content.

你能想到更好的解决方案吗?谢谢!

Can you think of better solution? Thank you!

推荐答案

通常的做法是顶层

<xsl:variable name="main-root" select="/"/>

要将主要输入文档节点存储在一个变量中,然后在具有来自不同文档的上下文节点的 for-each 或模板中,您始终可以访问该变量

to store the main input document node in a variable, then inside of a for-each or template with a context node from a different document you can always access the variable

<xsl:for-each select="$variableNode/root/element">
  <xsl:value-of select="$main-root//foo[@id = current()/@id]"/>
</xsl:for-each>

这篇关于Xpath:从每个循环的文档节点变量中选择节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆