xsl-list
[Top] [All Lists]

Re: Infinite Loop when param empty

2004-07-22 13:56:31
Hi Karl,

At 01:00 PM 7/22/2004, you wrote:
I have a param which is populated with an XML source.

The paramter is declared like this:
<xsl:param name="ENTRY_TEMPLATE" select="/"/>

My top level match is:
<xsl:template match="/">

Within this template rule I apply the following:
<xsl:apply-templates select="$ENTRY_TEMPLATE"/>

If the param ENTRY_TEMPLATE is not set, I end up with an infinite loop.  Is
this normal behaviour?  I'm guessing that maybe it is bad practice to
declare the param as I have.

It is normal behavior, since when your parameter is set to the root, selecting it and applying templates applies the template matching the root ... etc. ad infinitum.

But it's easy enough to set the parameter's default to something else. A node set containing no nodes will do nicely ... you could select="/parent::*" (or select="/.." if you prefer), which selects the parent of the root -- which doesn't exist. Accordingly in such cases templates will be applied to nothing, thereby avoiding the problem of the template selecting its own current node to apply itself to (thus selecting its own current node).

A more robust solution could actually trap the troublesome case by testing whether the parameter identifies the root:

<xsl:when test="generate-id(/) = generate-id($ENTRY_TEMPLATE)">
  <xsl:message>Sorry, can't process the root as entry template</xsl:message>
</xsl:when>
<xsl:otherwise>
  <xsl:apply-templates select="$ENTRY_TEMPLATE"/>
</xsl:otherwise>

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>