Re: [xsl] Breaking from the loop (for-each or apply-template)

Subject: Re: [xsl] Breaking from the loop (for-each or apply-template)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 12 Aug 2005 15:42:48 -0400
Hi,

The specification of xsl:message is soft, the instruction optional, and not all processors support it. I suppose this softness is simply out of a recognition that not all architectures are equally able to provide the functionality.

A more conventionally XSLT-ish way to approach the problem would be to define the error condition up front and simply design the stylesheet to return warnings instead of (or in addition to) the usual thing, if any error conditions obtain. So for example if I defined my condition as "all <value> elements must have content that casts to a number" (so, for example, "0" is okay but not just whitespace) and my input had (somewhere inside)

<value>5</value>
<value>9</value>
<value></value>
<value>Hooey!</value>

my XSLT could have

<xsl:variable name="errors" select="//value(not(number(.))"/>

<xsl:template match="/">
  <xsl:choose>
    <xsl:when test="$errors">
      <!-- erroneous 'value' elements are found -->
      <xsl:apply-templates select="$errors" mode="report-errors"/>
    </xsl:when>
    <xsl:otherwise>
      <!-- no errors: process normally -->
      <xsl:apply-templates/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

As noted above, you could leave out the xsl:choose and have the errors reported in addition to, rather than instead of, normal output.

Note that this approach only works on "errors" that can be defined ahead and that appear within otherwise proper well-formed XML.

Also, no "loop" is being "broken" -- no specification of flow of control is really needed here, just a plain old declaration that "if X is true, I want X', otherwise I want Y".

Cheers,
Wendell

At 03:20 PM 8/12/2005, you wrote:
Oleg,

    I've never used Cocoon, but I can tell you that Xalan 2.5.1 and
above (2.7 was released a few days ago) do support the xsl:message
instruction.

Regards,
Kenneth


======================================================================
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