An xslt code that worked in the production for several years failed unexpectedly. That's unusual, unfortunate but it happens.
We started to analyze the problem, limited the code block and recreated it in the simpe form. That's it:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="http://www.nesterovsky-bros.com/xslt/public" exclude-result-prefixes="t xs"> <xsl:template match="/" name="main"> <xsl:variable name="content"> <root> <xsl:for-each select="1 to 3"> <item/> </xsl:for-each> </root> </xsl:variable> <xsl:variable name="result"> <root> <xsl:for-each select="$content/root/item"> <section-ref name-ref="{t:generate-id()}.s"/> <!-- <xsl:variable name="id" as="xs:string" select="t:generate-id()"/> <section-ref name-ref="{$id}.s"/> --> </xsl:for-each> </root> </xsl:variable> <xsl:message select="$result"/> </xsl:template> <xsl:function name="t:generate-id" as="xs:string"> <xsl:variable name="element" as="element()"> <element/> </xsl:variable> <xsl:sequence select="generate-id($element)"/> </xsl:function> </xsl:stylesheet>
This code performs some transformation and assigns unique values to name-ref attributes. Values generated with t:generate-id() function are guaranteed to be unique, as spec claims that every node has its unique generate-id() value.
name-ref
t:generate-id()
generate-id()
Imagine, what was our surprise to find that generated elements all have the same name-ref's. We studied code all over, and found no holes in our reasoning and implementation, so our conlusion was: it's Saxon's bug!
It's interesting enough that if we rewrite code a little (see commented part), it starts to work properly, thus we suspect Saxon's optimizer.
Well, in the course of development we have found and reported many Saxon bugs, but how come that this little beetle was hiding so long.
We've verified that the bug exists in the versions 9.2 and 9.3. Here is the bug report: Saxon 9.2 generate-id() bug.
Unfortunatelly, it's there already for three days (2011-07-25 to 2011-07-27) without any reaction. We hope this will change soon.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u