xsl-list
[Top] [All Lists]

Re: [xsl] Calling a template before any other templates

2012-04-11 12:37:35
At 2012-04-11 13:29 -0400, Peter Desjardins wrote:
Hi. I am trying to do something in XSLT 2.0 that requires that some of
my templates know what the result of other templates is (or will be).
I want to run a process that writes a file before any other templates
run and then be able to refer to that file during the rest of the
processing.

That requires multiple invocations of the processor.

In XSLT 2.0 you can, however, put the results of a template into a variable and then work with the variable.

If I call a template from the first template like the following, can I
rely on it being completed before any other templates are applied? Is
there a reliable way to perform an operation before any templates are
applied?

I'm unclear why you are saying "before". If the template is writing a file, that file cannot be read subsequently regardless.

But, because your called template is being called before the other template rules fire, you can be assured that any building of the result tree is done before.

<xsl:template match="/">
  <xsl:call-template name="this.template.must.be.first" />
  <xsl:apply-templates/>
</xsl:template>

You can do the following in order to put the first template into a structure you can then access later as a parameter:

  <xsl:template match="/">
    <xsl:apply-templates>
      <xsl:with-param name="myStuff" tunnel="yes">
        <xsl:call-template name="blah"/>
      </xsl:with-param>
    </xsl:apply-templates>
  </xsl:template>

Then, in your match of any construct you can have:

  <xsl:template match="x">
    <xsl:param name="myStuff" tunnel="yes"/>

    .... $myStuff/y/z ....
  </xsl:template>

Alternatively, you can simply process the template into a global variable and then access the global variable. Regardless of how many times you access the global variable, it will only be evaluated once.

But you cannot, in either XSLT 1.0 or XSLT 2.0, create a result tree as an output file and then access that output file in the same invocation of the processor.

I hope this helps.

. . . . . . . . . Ken


--
Public XSLT, XSL-FO, UBL and code list classes in Europe -- May 2012
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>