问题
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>
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>
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()"/>
and got the desired elements by the doc-Function. ( doc($actual_filename)/ )
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!
回答1:
The usual approach is a top-level
<xsl:variable name="main-root" select="/"/>
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>
来源:https://stackoverflow.com/questions/19678459/xpath-select-node-from-within-document-node-variable-for-each-loop