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 |