RE: [xsl] XSLT transformation using counters

Subject: RE: [xsl] XSLT transformation using counters
From: Bubba O'Reily <killswi7ch1@xxxxxxxx>
Date: Wed, 27 Apr 2011 07:40:22 -0400
I want to export XML. There are other elements I also neeed to replace in the
file. If I output method="text" then those disappear in the output. For
example <a href="??">...</a>

I'm getting alot closer but how do I decrement the counter when a heading
appears outside of a </lev>. See example input, output & code below. Note the
output on the following lines:

<ParaStyle:hd4>Level 3
<ParaStyle:para>para text
<ParaStyle:hd4>Level 2
<ParaStyle:para>para text
<ParaStyle:hd4>Level 1
<ParaStyle:para>para text

The <ParaStyle:hd?> is not correct. I need to decrement the counter so when a
</lev> occurs the appropriate counter is used.

Should be:


<ParaStyle:hd3>Level 3
<ParaStyle:para>para text
<ParaStyle:hd2>Level 2
<ParaStyle:para>para text
<ParaStyle:hd1>Level 1
<ParaStyle:para>para text




Input:
------

<lev>
<heading>Level 1</heading>
<para>para text</para>
<heading>Level 1A</heading>
<para>para text</para>
 <lev>
  <heading>Level 2</heading>
  <para>para text</para>
  <heading>Level 2A</heading>
  <para>para text</para>
  <heading>Level 2B</heading>
  <para>para text</para>
  <lev>
   <heading>Level 3</heading>
   <para>para text</para>
   <lev>
    <heading>Level 4</heading>
    <para>para text</para>
    <heading>Level 4A</heading>
    <para>para text</para>
    <heading>Level 4B</heading>
    <para>para text</para>
   </lev>
   <heading>Level 3</heading>
   <para>para text</para>
  </lev>
  <heading>Level 2</heading>
  <para>para text</para>
 </lev>
 <heading>Level 1</heading>
 <para>para text</para>
</lev>




Output Required:
----------------


<ParaStyle:lev>
<ParaStyle:hd1>Level 1
<ParaStyle:para>para text
<ParaStyle:hd1>Level 1A
<ParaStyle:para>para text
<ParaStyle:lev>
<ParaStyle:hd2>Level 2
<ParaStyle:para>para text
<ParaStyle:hd2>Level 2A
<ParaStyle:para>para text
<ParaStyle:hd2>Level 2B
<ParaStyle:para>para text
<ParaStyle:lev>
<ParaStyle:hd3>Level 3
<ParaStyle:para>para text
<ParaStyle:lev>
<ParaStyle:hd4>Level 4
<ParaStyle:para>para text
<ParaStyle:hd4>Level 4A
<ParaStyle:para>para text
<ParaStyle:hd4>Level 4B
<ParaStyle:para>para text
<ParaStyle:hd3>Level 3
<ParaStyle:para>para text
<ParaStyle:hd2>Level 2
<ParaStyle:para>para text
<ParaStyle:hd1>Level 1
<ParaStyle:para>para text


Code:
-----

 <xsl:template match="@* | node()">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="lev">
  <xsl:text
disable-output-escaping="yes">&#13;&#10;&lt;ParaStyle:lev&gt;</xsl:text>
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="heading">
  <xsl:text
disable-output-escaping="yes">&#13;&#10;&lt;ParaStyle:hd</xsl:text>
  <xsl:number level="any" count="lev"/>
  <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="para">
  <xsl:text
disable-output-escaping="yes">&#13;&#10;&lt;ParaStyle:para&gt;</xsl:text>
  <xsl:apply-templates/>
 </xsl:template>






> Date: Tue, 26 Apr 2011 18:38:43 +0100
> From: davidc@xxxxxxxxx
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] XSLT transformation using counters
>
> On 26/04/2011 17:16, Bubba O'Reily wrote:
> >
> > <PStyle:hd1>
> > <PStyle:hd2>
> >
> > etc.
> >
>
> your sample results are not well formed XML, is that because you are not
> generating xml, in which case xsl:output method="text" is appropriate,
> or is it just that you have over-truncated your examples, in which case
> it's hard to suggest exactly what code you need without seeing a
> complete (but small) result xml file.
>
> david

Current Thread