Search code examples
xsltxalan

Why is generate-id() function returning the same id twice?


I have some XSLT that I didn't write but I have to maintain it. At some points it is using generate-id() to create an empty div which is then manipulated by a script further on. Problem is that the div is inside another div (i had thought this was a bit pointless, but acceptable anyway) and both are being given the same id! I tried this with Xalan and then again with the JRE transformer with the same result. I then tried to use the Eclipse/Xalan XSLT debugger and when i am stepping over the thing, it all works correctly...

The XSLT looks like this:

<xsl:template match="listed">
    <xsl:variable name="showwhat">
        <xsl:call-template name="IdentifyAudience"/>
    </xsl:variable>
    <xsl:if test="string-length($showwhat) &gt; 0">
        <div>
            <xsl:attribute name="id">
                <xsl:value-of select="generate-id()"/>   
            </xsl:attribute>
            <xsl:call-template name="Blah"/>
            <xsl:apply-templates/>
        </div>
    </xsl:if>
</xsl:template>

And the XML fragment is like this:

<listed id='ID39AF705AE17A000F337B000A' mode='html'>
    <ul>
        <li>Blah blah</li>
    </ul>
</listed>
<listed id='ID39AF715892AE000F337B002B' mode='html'>
    <ul>
         <li>Blech blech.</li>
    </ul>
</listed>

I appreciate this is a bit complicated - any clues? Please?


Solution

  • The problem is in the code you haven't show to us.

    generate-id() function return an unique identifier string for argument node or the context node otherwise.

    So, it's unique for unique node... You are probably calling the p/0 function in two content template with the same context node. How is this posible? Well...

    <xsl:call-template name="Blah"/> 
    

    call-template instruction does not change the context node