Re: [xsl] XHTML -to- WIKI ... so close ... yet not close enough

Subject: Re: [xsl] XHTML -to- WIKI ... so close ... yet not close enough
From: James Fuller <jim.fuller@xxxxxxxxxxxxxx>
Date: Sun, 06 Feb 2005 13:30:23 +0100
Alan Williamson wrote:

Morning one and all.

I am wishing to take XHTML to Wiki format. The Wiki format is relatively straight forward with no major complications. I have written most of the XSLT file but I am having problems (fun?) with working with embedded lists.

For example:

------ XHMTL -------------
<ul>
  <li>Element1</li>
  <li>Element2
    <ul>
       <li>Element2.1
         <ul>
           <li>Element2.1.1</li>
         </ul>
       </li>
    </ul>
  </li>
  <li>Element 3</li>
</ul>
-------------------------

Should be converted to:

------ WIKI -------------

- Element1
- Element2
-- Element2.1
--- Element2.1.1
- Element3

-------------------------

when solving these types of problems, its usually easier to create a 2 stage transformation...then optimise (e.g. fold everything into 1 step later on), try applying the following stylesheet to your xml snippet.

<xsl:stylesheet version = '1.0'
 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
 <xsl:output method="xml" indent="yes" />

 <xsl:template match="@*|comment()|processing-instruction()|text()">
   <xsl:copy>
     <xsl:apply-templates
select="*|@*|comment()|processing-instruction()|text()"/>
   </xsl:copy>
 </xsl:template>

   <xsl:template match="*">
       <xsl:element name="{name()}">
           <xsl:attribute name="depth" >
               <xsl:value-of select="count(ancestor-or-self::*)" />
           </xsl:attribute>
           <xsl:apply-templates
select="*|@*|comment()|processing-instruction()|text()"/>
       </xsl:element>
   </xsl:template>

</xsl:stylesheet>

you should get a copy of xml with a depth attribute node...the operative
term here doing the heavy lifting is the select statement
count(ancestor-or-self::*) which if u change it to recognize li should
get you the dashcount you want;

as in count(ancestor-or-self::li) should give you the right count....


<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method="xml" indent="yes" />

 <xsl:template match="@*|comment()|processing-instruction()|text()">
   <xsl:copy>
     <xsl:apply-templates
select="*|@*|comment()|processing-instruction()|text()"/>
   </xsl:copy>
 </xsl:template>

   <xsl:template match="*">
       <xsl:element name="{name()}">
           <xsl:attribute name="dashes" >
               <xsl:value-of select="count(ancestor-or-self::li)" />
           </xsl:attribute>
           <xsl:apply-templates
select="*|@*|comment()|processing-instruction()|text()"/>
       </xsl:element>
   </xsl:template>

</xsl:stylesheet>

this results in (using your xml);

<?xml version="1.0" encoding="utf-8"?>
<ul depth="0">
  <li dashes="1">Element1</li>
  <li dashes="1">Element2
       <ul dashes="1">
        <li dashes="2">Element2.1
               <ul dashes="2">
              <li dashes="3">Element2.1.1</li>
           </ul>
        </li>
     </ul>
  </li>
  <li dashes="1">Element 3</li>
</ul>

sine you are just interested in the dashes attribute on li attributes
you can ignore everything else now, with your 2nd stage transformation
just applying the dashes using whatever method.

is a good introduction to printing out things x number of times...look
at the hypen example...

http://www.xml.com/pub/a/2001/08/01/gettingloopy.html?page=2


good luck, Jim Fuller


Current Thread