xsl-list
[Top] [All Lists]

Re: Speeding up processing (with sablotron or saxon)

2004-07-13 11:34:50
> using these snippets of code results in nothing coming out of Saxon, and
> "Warning: Sablotron error on line 8: XSL element 'key' can only be used at
> the top level" error with Sablotron

What the processor error message is telling you is absolutely correct. All <xsl:key.../> elements must be top level elements and as such must be declared before your first <xsl:template...> element.

> first bit of code looking like this now
>
> ....
> <xsl:template match="server" />
> <xsl:template match="server[(_at_)name='Ahazi']">
> <resources>
> <xsl:key name="resource-by-id" match="resource" use="@swgcraft_id"/> <!-- this is where your error is -->

Here is the basic idea of what your stylesheet needs to look like...

<xsl:stylesheet...>
<xsl:key.../>
<!-- all <xsl:key.../> elements must be declared above -->
<xsl:template...>
        ...
<!-- if they are declared here they would be a descendant of <xsl:template...> which is why you see the error you are seeing -->
</xsl:template>
</xsl:stylesheet>

It can be difficult to look at the "use" and "match" attributes of <xsl:key../> and set aside the fact that, at the moment, the values have no context. I know for me this was the stumbling block that made me want to declare an <xsl:key.../> element where it was being used (thus allowing my mind to rest at ease knowing that the value of the match and use attributes were in the proper context) as opposed to somewhere that was completely outside the context of where I wanted it to be.

In reality the context of the attributes won't be known until runtime when the key function is called. It is then that the match and use attributes are bound to the current context within the XML data structure the stylesheet is processing. Although not the only reason implementing the functionality provided by the <xsl:key.../> and key function combination in this manner allows the reuse of the <xsl:key.../> element acting as a reference for the key function to create the desired data set from the current context as opposed to a hard coded data set.

Hope this helps!

Best regards,

<M:D/>