[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XHTML -to- WIKI ... so close ... yet not close eno

Subject: Re: XHTML -to- WIKI ... so close ... yet not close enough
From: James Fuller <jim.fuller@xxxxxxxxxxxxxx>
Date: Sun, 06 Feb 2005 13:30:23 +0100
li ul xhtml
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.