开发者

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开发者_Python百科: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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜