Re: [xsl] xsl:apply-imports design pattern using a single stylesheet

Subject: Re: [xsl] xsl:apply-imports design pattern using a single stylesheet
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 26 Apr 2004 17:45:46 -0400
Christian,

You could use modes to do this easily enough:

<xsl:template match="phrase" mode="output">
  <!-- a template to be called by other templates matching phrases,
       to write output -->
  <xsl:copy>
    <xsl:copy-of select="@*">
    <!-- doesn't have to be an identity template: could be anything -->
      <xsl:apply-templates/>
    </xsl:copy-of>
  <xsl:copy>
</xsl:templates>

<xsl:template match="phrase">
  <xsl:apply-templates select="." mode="output"/>
</xsl:template>

<xsl:template match="*[@revisionflag='deleted'">
  <deleted>
    <xsl:apply-templates select="." mode="output"/>
  </deleted>
</xsl:template>

etc.

Note you need to make sure the template matching *[@revisionflag='deleted']" has higher priority than the one matching the plain element "phrase", so it will be preferred when it can be used. (By default, it does, but e.g. if you changed "phrase" to "section/phrase" it wouldn't and you'd have to fix the priority explicitly.)

Using modes isn't either as graceful or as general as the apply-imports method, but it should work okay for your case.

Cheers,
Wendell

At 05:21 PM 4/26/2004, you wrote:
I have the problem to surround (almost) any element of a DTD (here:
DocBook) by an additional element based on one of its attributes. Example:

<phrase revisionflag="added">...</phrase>

should become

<inserted><phrase>...</phrase></inserted>

or

<para revisionflag="deleted">...</para>

should become

<deleted><para>...</para></deleted>


What I want is to have a mechanism like this pseudo-code:


<xsl:template match="*[@revisionflag]">
    if( revisionflag = "deleted" ) {

        <deleted>
            <xsl:apply-templates select=".", choosing among all
             defined templates except the one we're currently in />
        </deleted>

} else if ( revisionflag == "inserted" ) {

        <inserted>
            <xsl:apply-templates select=".", choosing among all
             defined templates except the one we're currently in />
        </inserted>

} else {

        <xsl:apply-templates select=".", choosing among all
         defined templates except the one we're currently in />

    }
</xsl:template>

, so that I have to code the wrapping of the element only at one place.
According to the FAQ mentioned above, this can be achieved by using two
stylesheets, where the main one contains my pseudo-coded template using
<xsl:apply-imports /> instead of my described pseudo-<xsl:apply-
templates> functionality, and the second, imported one contains the
normal, element-specific templates.

Is there a design pattern that lets me code this functionality in one
single stylesheet?


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread