Re: [xsl] creating and accessing element in the same stylesheet

Subject: Re: [xsl] creating and accessing element in the same stylesheet
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 16 Apr 2002 09:43:01 +0100
Hi Charly,

> Does anyone have any idea how to create an element in a template and
> accessing it with another template within the same stylesheet . I
> have a generic template that takes a specific format so I need to
> reformat my data before calling the generic template .

Whenever you create an set of XML using XSLT (1.0), it forms a result
tree fragment. To apply templates to nodes, they need to be in a node
set. The majority of processors have an extension function that
enables you to convert from a result tree fragment to a node set so
that you can process the information in the result tree fragment.

So, first you need to store the result tree fragment in a variable:

  <xsl:variable name="formatted">
    <root>
      <xsl:for-each select="/report/account">
        <account name="{@id}" value="{sum(detail/@value)}" />
      </xsl:for-each>
    </root>
  </xsl:variable>

Then you need to convert that to a node set using an extension
function, and apply templates to the 'root' element:

  <xsl;apply-templates select="exsl:node-set($formatted)/root" />

The extension function that you use depends on what processor you're
using. For MSXML, for example, you should use msxsl:node-set(). You
need to declare the relevant namespace in the xsl:stylesheet element.
For example, to use exsl:node-set(), you should have:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:exsl="http://exslt.org/common";
                exclude-result-prefixes="exsl">
...
</xsl:stylesheet>

Come XSLT 2.0 this will all be easier because there won't be such a
thing as "result tree fragments".

By the way, there's nothing in your code as you currently have it that
means that you need to go through this extra step; if you can, you
should avoid it because it's pretty expensive for the processor to
create new node sets, and because using an extension function prevents
your stylesheet from being portable across processors.

Cheers,

Jeni

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


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


Current Thread