Greetings:

 

I have a question that’s been bothering me for a long time. My understanding of how variables and parameters work in XSLT 1.1  is all goofed up. I do not understand why the stylesheet shown below produces the output it does. In particular, I’m confused about why the values of v1a and v1b are apparently identical.

 

Thank you in advance.

 

Cary Millsap

 

Stylesheet

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.1" xmlns:xsl=http://www.w3.org/1999/XSL/Transform exclude-result-prefixes="xsl">

            <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes" media-type="text/xml"/>

           

            <xsl:template match="/">

                        <xsl:variable name="v">

                                    <xsl:element name="a">

                                                <xsl:element name="b">

                                                            <xsl:element name="c"/>

                                                </xsl:element>

                                                <xsl:element name="b"/>

                                    </xsl:element>

                        </xsl:variable>

                       

                        <xsl:variable name="v1a"><xsl:call-template name="transform1"><xsl:with-param name="v" select="$v"/></xsl:call-template></xsl:variable>

                        <xsl:variable name="v1b"><xsl:call-template name="transform1"><xsl:with-param name="v" select="$v/*"/></xsl:call-template></xsl:variable>

                        <xsl:variable name="v1c"><xsl:call-template name="transform1"><xsl:with-param name="v" select="$v/*/*"/></xsl:call-template></xsl:variable>

                        <xsl:variable name="v1d"><xsl:call-template name="transform1"><xsl:with-param name="v" select="$v/*/*/*"/></xsl:call-template></xsl:variable>

                        <v1a><xsl:copy-of select="$v1a"/></v1a>

                        <v1b><xsl:copy-of select="$v1b"/></v1b>

                        <v1c><xsl:copy-of select="$v1c"/></v1c>

                        <v1d><xsl:copy-of select="$v1d"/></v1d>

                       

                        <xsl:variable name="v2a"><xsl:call-template name="transform2"><xsl:with-param name="v" select="$v"/></xsl:call-template></xsl:variable>

                        <xsl:variable name="v2b"><xsl:call-template name="transform2"><xsl:with-param name="v" select="$v/*"/></xsl:call-template></xsl:variable>

                        <xsl:variable name="v2c"><xsl:call-template name="transform2"><xsl:with-param name="v" select="$v/*/*"/></xsl:call-template></xsl:variable>

                        <xsl:variable name="v2d"><xsl:call-template name="transform2"><xsl:with-param name="v" select="$v/*/*/*"/></xsl:call-template></xsl:variable>

                        <v2a><xsl:copy-of select="$v2a"/></v2a>

                        <v2b><xsl:copy-of select="$v2b"/></v2b>

                        <v2c><xsl:copy-of select="$v2c"/></v2c>

                        <v2d><xsl:copy-of select="$v2d"/></v2d>

            </xsl:template>

           

            <xsl:template name="transform1">

                        <xsl:param name="v"/>

                        <xsl:copy-of select="$v"/>

            </xsl:template>

           

            <xsl:template name="transform2">

                        <xsl:param name="v"/>

                        <xsl:copy-of select="$v/*"/>

            </xsl:template>

           

</xsl:stylesheet>

 

Output (blank lines added for clarity)

<?xml version="1.0" encoding="UTF-8"?>

<v1a>

   <a>

      <b>

         <c/>

      </b>

      <b/>

   </a>

</v1a>

 

<v1b>

   <a>

      <b>

         <c/>

      </b>

      <b/>

   </a>

</v1b>

 

<v1c>

   <b>

      <c/>

   </b>

   <b/>

</v1c>

 

<v1d>

   <c/>

</v1d>

 

 

<v2a>

   <a>

      <b>

         <c/>

      </b>

      <b/>

   </a>

</v2a>

 

<v2b>

   <b>

      <c/>

   </b>

   <b/>

</v2b>

 

<v2c>

   <c/>

</v2c>

 

<v2d/>