[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Variables


Subject: RE: [xsl] Variables
From: Jeff Beadle <Jbeadle@xxxxxxxx>
Date: Fri, 1 Aug 2003 07:10:54 -0400

Hello Jose,

XML, and thus XSD and XSLT, is all about context ... or, if you will, scope.

Anything you define within an element will be "scoped" to that given element
... attributes and all descendants.

You can only get to an "inner" defined node by first accessing the "outer"
defined node.

The same is true for XSLT elements, in this case the xsl:for-each element.
Anything you do, construct, compute, whatever, inside that element will be
scoped to that element.

So, when you define a variable within a given for-each, the scope of that
given variable will be bounded by the for-each.

This conept is just like java, c++, c, vb, ... 

  void foo()
  {
     int x = 0;

     // ... work, work, work

  }

  void bar()
  {
    foo();

    int new_x = x; // ???

  }

now, will this work?  Will the reference "x" within the method bar be
resolved?  No, right(?), because it's not in scope ... it was declared
locally within foo.

This is no different than the following:

<xsl:template name="foo">
  <xsl:variable name="x" select="0"/>
 <!-- ... work, work, work -->

</xsl:template>

<xsl:template name="bar">
  <xsl:call-template name="foo"/>
  <xsl:variable name="new_x" select="$x"/> <!-- ??? -->

</xsl:template>



Back to your xsl.   Aside from the issue of context, I think you may have
another issue to tackle ... you said you want to be access "aname" outside
of the for-each.  Well, the unless you only have one
"Attribute_Groups/Attribute" visible within the context of your xsl:for-each
definition, you'll have more than one @Name, and judging by the singular
nature of the name you've given the variable ("aname") you may get
unexpected results.

So, the question is do you want a list of "@Name" nodes or only one.

If you want only one, then these should suffice:

   If you have one or more "Attribute_Groups/Attribute" elements, but you
don't care which one you pull the @Name from:
	
      <xsl:variable name="aname" select="Attribute_Groups/Attribute/@Name"/>

   If you do care, then you'll want to position your @Name selection ...
something like:

      <xsl:variable name="aname" select="Attribute_Groups/Attribute[ last()
]/@Name"/>

      or,

      <xsl:variable name="aname" select="Attribute_Groups/Attribute[
position() = 1]/@Name"/>


If you want a list of @Names then you'll need either copy the parent element
or create new elements or change the attribute to an element:

   Define the variable outside of the for-each, this example copies the
parent element and only the "Name" attribute:

      <xsl:variable name="anames">
         <xsl:for-each select="Attribute_Groups/Attribute">
            <xsl:copy>
	   <xsl:copy-of select="@Name"/>
	</xsl:copy>
         </xsl:for-each>
      </xsl:variable>


Anyway, I hope some of this helps to shed some light.

Cheers,
Jeff


-----Original Message-----
From: AROSO Jose Antonio [mailto:jose.santos@xxxxxxxxxxx]
Sent: Friday, August 01, 2003 6:34 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Variables


Hi..


When I save something in a variable in a loop for-each i dont have access to
this variable out of the loop for-each.
How can i resolve this situation.
In the following example:

<xsl:for-each select="Attribute_Groups/Attribute">

	<xsl:variable name="aname" select="@Name" />
	........
</xsl:for-each>

How can i access the variable aname out of the loop for-each?


Best Regards

Jose





 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
Keywords