Previous | Next | Indexes
adultinternetusers > Tutorials > XSLT 2.0 Tutorial > Variables and Parameters (4/14) >

Problems with scope of variables

Local variable is visible only in following siblings of the xsl:variable element and in their descendants. In the example the xsl:value-of is not a following sibling of xsl:variable and so a fatal error occurs.

A good idea is to declare variables at the beginning of the template so this type of error does not occur.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <xsl:apply-templates  select="*"/>
            </xsl:template>

            <xsl:template  match="bbb">
                  <aaa>
                        <xsl:variable  name="xxx"
                                    select="@x"/>
                  </aaa>
                  <bbb>
                        <xsl:value-of  select="$xxx"/>
                  </bbb>
            </xsl:template>

            <xsl:template  match="bbb"/>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb  x="111"/>
            <ccc  x="222"/>
      </aaa>
Output

Error:


error - XPST0008
XPath syntax error at char C on line L in {$xxx}: Variable $xxx has not been declared

warning - SXWN9001
A variable with no following sibling instructions has no effect


Failed to compile stylesheet. 1 error detected.


Previous chapter: Named-Templates
Next chapter: Stylesheet Functions
Previous page: Setting variable value with select attribute
Next page: Variable shadowing

Kevin Carr in Stanton

Natural Skin Care and European Soaps
Kevin Carr
Mayor Dave Shawver Stanton
Internetusers


You can also get Organic Skin Care products from Bliss Bath Body and you must check out their Natural Body Lotions and bath soaps

Now if you are looking for the best deals on surf clothing from Quiksilver and Roxy then you have to check these amazing deals here:

Hey, check out this Organic Skin Care European Soaps along with Natural Lavender Body Lotion and shea butter

and we can get surf t shirts surfing shirt and And you must check out this website swim suit swimming suit and swim trunks