Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How to transform recusrive XML data with XSL?

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 3/11/2005 1:59:00 PM

David Bowey wrote:


> I've the following "recursive" XML file...
> 
> ###### XML FILE ######
> 
> <?xml version="1.0" encoding="utf-8" ?>
> <Nodes>
>  <Node>
>   <text>Parent 1</text>
>   <Nodes>
>    <Node>
>     <text>Child 1</text>
>     <Nodes>
>      <Node>
>       <text>Grandchild 1</text>
>      </Node>
>     </Nodes>
>    </Node>
>    <Node>
>     <text>Child 2</text>
>    </Node>
>   </Nodes>
>  </Node>
>  <Node>
>   <text>Parent 1</text>
>   <Nodes>
>    <Node>
>     <text>Child 1</text>
>     <Nodes>
>      <Node>
>       <text>Grandchild 1</text>
>      </Node>
>     </Nodes>
>    </Node>
>    <Node>
>     <text>Child 2</text>
>    </Node>
>   </Nodes>
>  </Node>
> </Nodes>
> 
> ###### END XML FILE ######
> 
> How do I transform this XML data into XHTML bulleted list using XSL?

That is rather easy with XSLT, you simply write templates for each of 
the different elements you have above creating the needed XHTML elements 
(e.g. <ul>, <li>) and then recursively with apply-templates process the 
remaining nodes:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/1999/xhtml"
   version="1.0">

<xsl:output method="xml"
             indent="yes"
             encoding="UTF-8"
             doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
 
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
             omit-xml-declaration="yes" />

<xsl:template match="/">
   <html xml:lang="en" lang="en">
     <head>
       <title>list</title>
     </head>
     <body>
       <xsl:apply-templates />
     </body>
   </html>
</xsl:template>

<xsl:template match="Nodes">
   <ul>
     <xsl:apply-templates />
   </ul>
</xsl:template>

<xsl:template match="Node">
   <li>
     <xsl:apply-templates />
   </li>
</xsl:template>

<xsl:template match="text">
   <span><xsl:value-of select="." /></span>
</xsl:template>

</xsl:stylesheet>

-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent