[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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
xsl change value of variable
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.