sep
sep

Reputation: 3495

How to refer to the input document context within the context of an external document?

<xsl:template match="extnode">
    <xsl:if test="/topnode/value">

    </xsl:if>
</xsl:template>

<xsl:template match="/">
     <xsl:apply-template select="document('external.xml')/exttopnode/extnode"/>
</xsl:template>

In the example above, the context on line 2 (xsl:if) will be with respect to document('external.xml'). But what I really want is to test an element from the input XML. Is there a way to refer to the input document?

Currently I'm forced to pass the entire node tree of the input document as an argument to the template, and I was wondering if there is a better way.

Upvotes: 1

Views: 796

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

In the example above, the context on line 2 (xsl:if) will be with respect to document('external.xml'). But what I really want is to test an element from the input XML. Is there a way to refer to the input document?

Whenever I find myself in such situation, I prefer to have a global variable (say named $vMainDoc) that is accessible anywhere in the whole transformation without the need to pass a parameter:

<xsl:variable name="vMainDoc" select="/"/>

Then your code would become:

<xsl:template match="extnode">
    <xsl:if test="$vMainDoc/topnode/value">

    </xsl:if>
</xsl:template>

Upvotes: 2

Related Questions