OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help

docbook-apps message

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]

Subject: Re: [docbook-apps] variables in parents : xsl

On Sun, Jul 04, 2004 at 11:42:39PM +0200, Wim Lemkens wrote:
> This maybe isn't entirely the correct place to ask.
> But how can I access variables from parents in xsl?
> Eg.: I have a custom list where the list itself has an id. Now I want to be 
> able to let every listitem now the id of that list.
> <xsl:template match="customlist">
>   <xsl:variable name="listid">
>     <xsl:value-of select="generate-id()"/>
>   </xsl:variable>
> </xsl:template>
> <xsl:template match="customlist/listitem">
>   <xsl:copy-of select="$listid"/>
> </xsl:template>

Your question, and your examples, display a subtle nuance about XSLT
programming that I will attempt to address here.  To make things
concrete, let's use the following made-up XML document:


If you are working within the context of the body, say within the
following template:

    <xsl:template match="body">
        <!-- Do stuff -->

Then XSLT will begin processing all the templates that match, in tree
order.  In this case, your first template would match the customlist,
and then your second template would match the customlist/listitem, both
from the context of the body element, because both match in the context
of the body element.  However, there is nothing shared between those two
templates; the variable in the first is not seen by the second.

You need to take advantage of XSLT's recursive nature, and process the
listitem as a child of the customlist in order to pass data from the
latter to the former.  It might look something like this:

    <xsl:template match="customlist">
        <xsl:variable name="listid">
            <xsl:value-of select="generate-id()"/>
        <xsl:apply-templates select="listitem">
            <xsl:with-param name="listid" select="{$listid}"/>

    <xsl:template match="listitem">
        <xsl:param name="listid"/>
        <!-- Do stuff with listid here... -->

Note how listitem is matched without any context; this gives its parent
the freedom to match it against its children directly, as done above.
This is probably best explained a different way; still, I hope this was

Take care,

    John L. Clark

PGP signature

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]