xsl-list
[Top] [All Lists]

Re: Writing an optional element

2004-07-05 14:12:35
At 03:41 PM 7/5/2004, it was written:
   Nestor> Is there a better way to do it than:

    Nestor> <xsl:choose> <xsl:when test="condition"> <element>
    Nestor> ... processing instructions ...  </element> </xsl:when>
    Nestor> <xsl:otherwise> ... repeat processing instructions ...
    Nestor> </xsl:otherwise> </xsl:choose>

If by a better way, you mean you want to avoid writing the same
"continue processing" code, then you could call a named template
each time. Said named template to contain the common code.

Also, there are times when conditional logic can be handled by template matches:

<xsl:template match="span[(_at_)display='i']">
  <i>
    <xsl:apply-templates/>
  </i>
</xsl:template>

<xsl:template match="span">
  <xsl:apply-templates/>
</xsl:template>

Because the first template has a higher priority than the second, it will fire in preference to the second when the condition in the predicate is true; the second template catches those cases for which the condition isn't true.

Not every condition can be handled like this, due to restrictions on what may appear in patterns (e.g. no variable references are allowed). Nevertheless this is very useful, and certainly clean compared to those somewhat bloated choose/when/otherwise combinations.

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
======================================================================



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