GNOME Bugzilla – Bug 58444
Bug in local variable scoping?
Last modified: 2009-08-15 18:40:50 UTC
In the following stylesheet, I expect the two <xsl:value-of> calls to result different results, but it doesn't. Checking with the local guru and against Saxon and Xalan, we think libxslt is wong here. The stylesheet: <?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="message"> <ROOT> <ELEMENT1> <xsl:variable name="test-var"> <xsl:call-template name="test1"/> </xsl:variable> <xsl:value-of select="$test-var"/> </ELEMENT1> <ELEMENT2> <xsl:variable name="test-var"> <xsl:call-template name="test2"/> </xsl:variable> <xsl:value-of select="$test-var"/> </ELEMENT2> </ROOT> </xsl:template> <xsl:template name="test1"> Some result </xsl:template> <xsl:template name="test2"> other one </xsl:template> </xsl:stylesheet> The XML-Input: <?xml version="1.0" encoding="iso-8859-1"?> <message/> Results from running various XSLT-Processors bash-2.05$ !xs xsltproc foo.xsl foo.xml xsl:variable : redefining test-var <?xml version="1.0"?> <ROOT><ELEMENT1> Some result </ELEMENT1><ELEMENT2> Some result </ELEMENT2></ROOT> bash-2.05$ testXSLT -in foo.xml -xsl foo.xsl ========= Parsing foo.xsl ========== Parse of foo.xsl took 10 milliseconds ========= Parsing foo.xml ========== Parse of foo.xml took 0 milliseconds ============================= Transforming... <?xml version="1.0" encoding="UTF-8"?> <ROOT><ELEMENT1> Some result </ELEMENT1><ELEMENT2> other one </ELEMENT2></ROOT> transform took 0 milliseconds Total time took 0 milliseconds develop@projects: saxon foo.xml foo.xs l <?xml version="1.0" encoding="utf-8"?><ROOT><ELEMENT1> Some result </ELEMENT1><ELEMENT2> other one </ELEMENT2></ROOT> This is with libxslt 1.0.0, but also shows with 1.0.1
Right, this is a libxslt bug. Redefining the same variable twice in a template is not supported, the easiest solution to work around this being to use different names. It's also a matter of style but defining the variables at the top level of the template as the first instructions makes the XSLt code really cleaner (i.e. read/understand/maintain). Fixing it won't be that easy too, unless this is really a blocking point it may not be fixed quickly. Please tell if this is really blocking your work. Daniel
Dohh it was actually really easy to fix, I should not have postponed handling it so long: http://cvs.gnome.org/bonsai/cvsquery.cgi?module=libxslt&branch=HEAD&branchtype=match&dir=libxslt&file=&filetype=match&who=veillard&whotype=match&sortby=Date&hours=&date=explicit&mindate=02%2F11%2F02+08%3A13&maxdate=02%2F11%2F02+08%3A15&cvsroot=%2Fcvs%2Fgnome thanks for the bug report, and sorry for the delay Daniel
Should be fixed in libxslt-1.0.12 thanks, Daniel