Referring input document when processing multiple documents in XSLT

  • Thread starter Filip Hendrickx
  • Start date
F

Filip Hendrickx

Hi all.

I am processing two documents with an XSLT, one is the input document,
the other is loaded with the document() function in a variable $doc2.

Inside a template that matches an element from $doc2, I need to check a
value from the original input document. However, inside this template,
the context is $doc2. What XPath expression should I use to refer to the
input document? I've tried using /inputNamespace:inputElement, where
inputNamespace and inputElement refer to the input document. This
doesn't work because no such element exists within the context of the
template, which is $doc2.

A small example may clarify want I mean :).

<xsl:stylesheet>
<xsl:template match="/">
<xsl:variable name="doc2" select="document('doc2.xml')"/>
<xsl:apply-templates select="$doc2/someNodeInDoc2"/>
</xsl:template>

<xsl:template match="someNodeInDoc2">
<xsl:variable name="someNodeInInputDoc" select="XPath expression
referring input document"/>
...
</xsl:template>
</xsl:stylesheet>

I solved the problem by defining an additional variable $inputDoc for
the input document and then using $inputDoc/someNodeInInputDoc, but
isn't there a way to directly access the input document, without
explicitly having to load the input document?

Thanks!

Filip Hendrickx.
 
G

George Bina

Hi Filip,

The solution is exactly what you used to solve the problem: define a
variable to point to the initial document and use that to access
information in that document.

Best Regards,
George
 
J

Joe Kesselman

Filip said:
I solved the problem by defining an additional variable $inputDoc for
the input document and then using $inputDoc/someNodeInInputDoc

I think a global variable set to reference the root of the main input
document really is your best solution. This will get set at stylesheet
startup, so it's nearly as efficient as if there was an XSLT built-in
function for this purpose.

<xsl:stylesheet>
<xsl:variable name="inputDoc" select="/"/>

<xsl:template match="/">
<xsl:variable name="doc2" select="document('doc2.xml')"/>
<xsl:apply-templates select="$doc2/someNodeInDoc2"/>
</xsl:template>

<xsl:template match="someNodeInDoc2">
<xsl:variable name="someNodeInInputDoc"
select="$inputDoc/someNodeInInputDoc"/>
...
</xsl:template>
</xsl:stylesheet>
 
F

Filip Hendrickx

Joe Kesselman schreef:
I think a global variable set to reference the root of the main input
document really is your best solution. This will get set at stylesheet
startup, so it's nearly as efficient as if there was an XSLT built-in
function for this purpose.

<xsl:stylesheet>
<xsl:variable name="inputDoc" select="/"/>

<xsl:template match="/">
<xsl:variable name="doc2" select="document('doc2.xml')"/>
<xsl:apply-templates select="$doc2/someNodeInDoc2"/>
</xsl:template>

<xsl:template match="someNodeInDoc2">
<xsl:variable name="someNodeInInputDoc"
select="$inputDoc/someNodeInInputDoc"/>
...
</xsl:template>
</xsl:stylesheet>

Now, why did I overlook this simple and elegant solution ;-)? Thanks!

Filip.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top