Re: [xsl] Templates, variables, and tree fragments

Subject: Re: [xsl] Templates, variables, and tree fragments
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 24 Aug 2002 17:24:52 +0100
Hi Kimberly,

> The below is a cut down version of the problem. The template for
> KH/Key/MD/@AskAvl can change the value in the attribute. The output
> of the 'row' node produced in the transform ion (RowMD template)
> shows the starting XML value of @AskAvl. But, I need the @AskAvl
> that was produced in the rebuilt 'MD' node (KH/Key/MD template)
[snip]
> I read a snippet somewhere (don't remember where I saw it) that indicated
> variables could call templates, so I tried -
>         <xsl:template name="RowMD">
>                 <xsl:variable name="Market">
>                         <xsl:element name="MD">
>                                 <xsl:apply-templates select="@*"/>
>                         </xsl:element>
>                 </xsl:variable >
>
> (When RowMD is called the current node is an MD node.)  This produces an
> error stating that the market variable does not have a nodeset.

That's right. When you set a variable using its content rather than
the select attribute (whether you set the variable by calling or
applying a template or through some other mechanism) then you create a
"result tree fragment". Result tree fragments are like mini result
trees -- a root node with some elements or text as children.

Unfortunately, you can't index into a result tree fragment like you
can into the *source tree*. To do that, you need to first turn it into
a node set, and to do that you need to use an extension function. I
don't know what processor you're using, but if you take a look at its
documentation it should somewhere tell you how to use its
"foo:node-set" extension function (where 'foo' is a prefix that's
associated with a namespace that depends on the processor). Once
you've declared the namespace for the extension function at the top of
your stylesheet, you can use the foo:node-set() extension function for
example to do:

  <xsl:value-of select="foo:node-set($Market)/MD/@AskAvl" />

to get the value of the AskAvl attribute on the new MD element that
you've just created.

If you need help getting the node-set() extension function working,
tell us what processor you're using and we'll be glad to assist.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread