Re: [xsl] outputting unknown amount of child elements

Subject: Re: [xsl] outputting unknown amount of child elements
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 5 Feb 2004 12:35:06 +0000
Hi David,

> How can I process this so all child elements (regardless of the
> depth) are outputted in a way that mirrors the structure structure
> of the original document.

Write a template that matches the given element, and within that
template, apply templates to the children of the element. For example,
something like:

<xsl:template match="item">
  <!-- Output the word 'Heading' if the item has child items -->
  <xsl:if test="item">Heading </xsl:if>
  <!-- Apply templates to the children of this item, including the
       text -->
  <xsl:apply-templates />

You don't have to know how many levels there might be: the template
keeps being applied recursively down the tree.

By the way, if you have control over the design of the XML that you're
using, you might consider changing it so that the <item> elements that
contain other <item> elements wrap the heading in a <heading> (or
similar) element. Having mixed content in such structures is usually
bad news when it comes to XSLT processing.



Jeni Tennison

 XSL-List info and archive:

Current Thread