Re: Change the value of global variables/params ??

Subject: Re: Change the value of global variables/params ??
From: Paul Levin <plevin@xxxxxxx>
Date: Wed, 08 Dec 1999 09:55:32 -0500
David,
    Please consider the following XML and XSL.  Note that I have no control
over the XML.  That is, it is what it is, I can not have the structure of
the XML changed.
    Note also that the following XML and XSL has been "cut-down" to only
show those parts that are relevant to our discussion.  Also the tag names
have been changed to protect our information.
    You will see in the XSL below, that I use a "changable" variable to
allow grouping.  Can you tell me another way to produce the same output
without a "changable" variable ?

    Paul

-----------------------------  Sample XML ----------------------
<?xml version="1.0"?>

<TOP>
    <LEVEL1>
        <LEVEL2>
            <GROUP>

                <ITEM>
                    <NAME>Name1</NAME>
                    <INFO>
                        <INFOTYPE>
                            <ID1>001</ID1>
                            <ID2>001</ID2>
                            <ITEMTYPE>TYPE1</ITEMTYPE>
                        </INFOTYPE>
                    </INFO>
                </ITEM>

                <ITEM>
                    <NAME>Name2</NAME>
                    <INFO>
                        <INFOTYPE>
                            <ID1>002</ID1>
                            <ID2>002</ID2>
                            <ITEMTYPE>TYPE1</ITEMTYPE>
                        </INFOTYPE>
                    </INFO>
                </ITEM>

                <ITEM>
                    <NAME>Name3</NAME>
                    <INFO>
                        <INFOTYPE>
                            <ID1>003</ID1>
                            <ID2>003</ID2>
                            <ITEMTYPE>TYPE2</ITEMTYPE>
                        </INFOTYPE>
                    </INFO>
                </ITEM>

                <ITEM>
                    <NAME>Name4</NAME>
                    <INFO>
                        <INFOTYPE>
                            <ID1>004</ID1>
                            <ID2>004</ID2>
                            <ITEMTYPE>TYPE2</ITEMTYPE>
                        </INFOTYPE>
                    </INFO>
                </ITEM>

            </GROUP>
        </LEVEL2>
    </LEVEL1>
</TOP>

-----------------------------  Sample XSL  ----------------------


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>

<!--
    For the above XML, generate the following HTML

    <HTML>
        <BODY>
            <H1>TYPE1</H1>
                <P>Name1</P>
                <P>Name2</P>
            <H1>TYPE2</H1>
                <P>Name3</P>
                <P>Name4</P>
        </BODY>
    </HTML>
-->

 <xsl:output method="html"/>

 <xsl:template match="/">
     <HTML>

     <BODY>

        <!-- initialize our changing variable -->
       <xsl:variable name="last_type" select="'none'"
/>                                <!-- initialize variable -->

       <!-- for each item -->
       <xsl:for-each select="//ITEM">

            <!-- sort the list of items first by type, and then by name -->
            <xsl:sort select=".//ITEMTYPE" data-type="text"
order="ascending"/>
            <xsl:sort select=".//NAME" data-type="text" order="ascending"/>

            <!-- if this item's type is different than the previous item's
type
            <xsl:if test="$last_type !=
.//ITEMTYPE">                                <!-- test variable -->

                 <!-- update the last_type variable -->
                 <xsl:variable name="last_type"
select=".//ITEMTYPE"/>        <!-- change variable -->

                <!-- output the new type -->
                <H1><xsl:value-of select=".//ITEMTYPE"/></H1>
           </xsl:if>

            <!-- output the item name -->
            <P><xsl:value-of select=".//NAME"/></P>

       </xsl:for-each>

     </BODY>
     </HTML>
 </xsl:template>

</xsl:stylesheet>



David Carlisle wrote:

> <xsl:template name="sub">
>     <xsl:variable name="foo" select="'abc'"/>
>     <xsl:value-of select="$foo"/>
>     <xsl:variable name="foo" select="'def'"/>
>     <xsl:value-of select="$foo"/>
> </xsl:template>
>
> Calling this template will output  --   abcdef
>
> that was legal xsl until one of the later drafts, October perhaps
> can't remember exactly when they decided to ban such `shadowing'.
> There is no way you can ever need such a feature as it is always
> possible to change the second variable name to foo2. You are
> not really changing the value f foo, just declaring a new variable with
> the same name that `hides' the first. As it is always possible to tell
> just from the position in the template, which variable is being
> referenced by $foo no functionality is lost by insisting that the second
> variable has a different name.
>
> David
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread