Recursive substitution

Subject: Recursive substitution
From: Peter Quarendon <Pq@xxxxxxxxx>
Date: Thu, 11 Mar 1999 15:29:49 +0000
Can anyone suggest a way to re-code the following horrible stylesheet
fragment:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl";                
                xmlns="http://www.w3.org/TR/REC-html40";> 

<xsl:template match="/">
    <xsl:element name='PARAM'>
         <xsl:attribute name='name'>tree</xsl:attribute>
         <xsl:attribute name='value'><xsl:apply-templates/></xsl:attribute>
   </xsl:element>   
</xsl:template>
<!-- attempt to put a number of $'s corresponding to the nesting depth -->
<!-- the mode indicates the depth of nesting:                          -->
<xsl:template match='CGROUP'><xsl:apply-templates mode='$' select='CGROUP |
CITEM'/></xsl:template>
<xsl:template match='CGROUP' mode='$'><xsl:apply-templates mode='$$'
select='CGROUP | CITEM'/></xsl:template>
<xsl:template match='CGROUP' mode='$$'><xsl:apply-templates mode='$$$'
select='CGROUP | CITEM'/></xsl:template>
<xsl:template match='CGROUP' mode='$$$'><xsl:apply-templates mode='$$$$'
select='CGROUP | CITEM'/></xsl:template>
<xsl:template match='CGROUP' mode='$$$$'><xsl:apply-templates mode='$$$$$'
select='CGROUP | CITEM'/></xsl:template>
<xsl:template match='CGROUP' mode='$$$$$'>Warning: Tree nesting too
deep!</xsl:template>
<!-- output a number of $s according to the mode, followed by the rest of
the text -->
<xsl:template match='CITEM'><xsl:apply-templates/>|
</xsl:template>
<xsl:template match='CITEM' mode='$'>$<xsl:apply-templates
select='.'/></xsl:template>
<xsl:template match='CITEM' mode='$$'>$$<xsl:apply-templates
select='.'/></xsl:template> 
<xsl:template match='CITEM' mode='$$$'>$$$<xsl:apply-templates
select='.'/></xsl:template>
<xsl:template match='CITEM' mode='$$$$'>$$$$<xsl:apply-templates
select='.'/></xsl:template>
<xsl:template match='CITEM' mode='$$$$$'>$$$$$<xsl:apply-templates
select='.'/></xsl:template> 
</xsl:stylesheet>

which just puts a number of $'s corresponding to the CGROUP depth in front
of every CITEM.
For instance from:

<?xml version="1.0"?> 
    <CGROUP>
      <CITEM>depth 1</CITEM>
      <CITEM>depth 1</CITEM>
      <CGROUP>
       <CITEM>depth 2</CITEM>
       <CITEM>depth 2</CITEM>              
      </CGROUP>
      <CITEM>depth 1</CITEM> 
      <CITEM>depth 1</CITEM>
    </CGROUP>

it generate:

<PARAM name="tree" value="$depth 1|
$depth 1|
$$depth 2|
$$depth 2|
$depth 1|
$depth 1|"/>  

I feel there should be a proper way to get the same result (without
excaping to script). 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread