Ok,
I'm using Saxon 6.5.2 and the code presented below is working fine.
Here is the stylesheet(I changed it slightly by adding debug-messages):
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:param name="A" select="'document.xml'"/>
<xsl:param name="B" select="'guideDocument.xml'"/>
<xsl:variable name="aRootNode" select="document($A)"/>
<xsl:variable name="bRootNode" select="document($B)"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:call-template name="recursiveTemplate">
<xsl:with-param name="aNode" select="$aRootNode/*[1]"/>
<xsl:with-param name="bNode" select="$bRootNode/*[1]"/>
<xsl:with-param name="depth" select="1"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="recursiveTemplate">
<xsl:param name="aNode"/>
<xsl:param name="bNode"/>
<xsl:param name="depth"/>
<p>
[recursiveTemplate called in depth <xsl:value-of select="$depth"/> !]
</p>
<xsl:call-template name="doStuffwAttributes">
<xsl:with-param name="aNode" select="$aNode"/>
<xsl:with-param name="bNode" select="$bNode"/>
</xsl:call-template>
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">
<xsl:call-template name="auxillaryTemplate">
<xsl:with-param name="aNode" select="$aNode/*[name() = name(current())]"/>
<xsl:with-param name="bNode" select="$bNode/*[name() = name(current())]"/>
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="recursiveTemplate">
<xsl:with-param name="aNode" select="$aNode/*[name() = name(current())]"/>
<xsl:with-param name="bNode" select="$bNode/*[name() = name(current())]"/>
<xsl:with-param name="depth" select="$depth + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="doStuffwAttributes">
<xsl:param name="aNode"/>
<xsl:param name="bNode"/>
<p>
[doStuffwAttributes called!]<br/>
current node is <b><xsl:value-of select="name(current())"/></b>
</p>
doStuffwAttributes: aNode contains <xsl:value-of select="count($aNode)"/> node(s). <br/>
doStuffwAttributes: bNode contains <xsl:value-of select="count($bNode)"/> node(s). <br/>
<xsl:for-each select="@*">
doStuffwAttributes (inside for-each): aNode contains <xsl:value-of select="count($aNode)"/> node(s). <br/>
doStuffwAttributes (inside for-each): bNode contains <xsl:value-of select="count($bNode)"/> node(s). <br/>
</xsl:for-each>
</xsl:template>
<xsl:template name="auxillaryTemplate">
<xsl:param name="aNode"/>
<xsl:param name="bNode"/>
<xsl:param name="depth"/>
<p>
[auxillaryTemplate called!]<br/>
current node is <b><xsl:value-of select="name(current())"/></b>
</p>
<xsl:for-each select="*">
<xsl:call-template name="recursiveTemplate">
<xsl:with-param name="aNode" select="$aNode/*[name() = name(current())]"/>
<xsl:with-param name="bNode" select="$bNode/*[name() = name(current())]"/>
<xsl:with-param name="depth" select="$depth + 1"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
where
1) source XML doc to which the stylesheet is applied is:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root attr="a0">
<child1 attr1="a1" attr2="a2">
<child11 attr11="a11" attr2="a2" attr3="a3"/>
</child1>
<child2 attr1="a1" attr2="a2">
<child21 attr11="a11" attr2="a2" attr3="a3"/>
</child2>
</root>
1) document.xml is:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<x-el1>initial element 1</x-el1>
<x-el2>initial element 2</x-el2>
</doc>
3) guideDocument.xml is:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<guide>
<el1>element 1</el1>
<el2>element 2</el2>
</guide>
and the output I get is(the output is malformed HTML, which is just a debug-stuff):
*******************************************
[recursiveTemplate called in depth 1 !]
[doStuffwAttributes called!]
current node is root
doStuffwAttributes: aNode contains 1 node(s).
doStuffwAttributes: bNode contains 1 node(s).
doStuffwAttributes (inside for-each): aNode contains 1 node(s).
doStuffwAttributes (inside for-each): bNode contains 1 node(s).
[recursiveTemplate called in depth 2 !]
[doStuffwAttributes called!]
current node is child1
doStuffwAttributes: aNode contains 0 node(s).
doStuffwAttributes: bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
[recursiveTemplate called in depth 3 !]
[doStuffwAttributes called!]
current node is child11
doStuffwAttributes: aNode contains 0 node(s).
doStuffwAttributes: bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
[auxillaryTemplate called!]
current node is child2
[recursiveTemplate called in depth 2 !]
[doStuffwAttributes called!]
current node is child21
doStuffwAttributes: aNode contains 0 node(s).
doStuffwAttributes: bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
doStuffwAttributes (inside for-each): aNode contains 0 node(s).
doStuffwAttributes (inside for-each): bNode contains 0 node(s).
************************************
The output shows(the first pass of the recursion) that the values of those variables are the same inside and outside the xsl:for-each.
To get richer output, you can change the XPath expressions $aNode/*[name() = name(current())] and $bNode/*[name() = name(current())], or alternatively change the appropriate element types in the input XML documents.
If your output is different from mine, it probably means that there is a bug in the XSLT processor you're using (the output I get here is expected one, and also Saxon 6.5.2 is reliable enough upon my experience).
Regards,
Armen