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

Re: arbitrary depth element type conversion

Subject: Re: arbitrary depth element type conversion
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Fri, 3 Feb 2006 21:47:22 +0530
div depth
Please try this stylesheet

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

<xsl:output method="xml" indent="yes" />

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

<xsl:template match="*" priority="1">
   <div class="{name()}">
     <xsl:apply-templates />
   </div>
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

On 2/3/06, Sebastian Tennant <sebyte@xxxxxxxxxxxxxxx> wrote:
> Hi all,
>
> Given an XML doc:
>
>  <document>
>    <first>
>      first
>      <first-child-of-first>
>        first child of first
>      </first-child-of-first>
>      <second-child-of-second>
>        second child of second
>      </second-child-of-second>
>    </first>
>    <second>
>      second
>      <first-child-of-second>
>        first child of second
>        <first-grandchild-of-second>
>          first grandchild of second
>        </first-grandchild-of-second>
>      </first-child-of-second>
>    </second>
>    <third>
>      third
>    </third>
>    orphan
>  </document>
>
> I would like to preserve the exact same structure but change all the
> elements to divs, with class attributes reflecting the source element
> types, to an arbitrary depth.
>
> I have almost got there with this stylesheet:
>
>  <xsl:template match="document">
>      <xsl:apply-templates select="child::*" />
>  </xsl:template>
>
>  <xsl:template match="*">
>    <xsl:element name="div">
>      <xsl:attribute name="class">
>        <xsl:value-of select="name(.)" />
>      </xsl:attribute>
>      <xsl:value-of select="." />
>      <xsl:apply-templates select="child::*" />
>    </xsl:element>
>  </xsl:template>
>
> but the element contents of child elements are being duplicated in
> parent elements:
>
>  <div class="first">
>    first
>    first child of first
>    second child of second
>    <div class="first-child-of-first">
>      first child of first
>    </div>
>    <div class="second-child-of-second">
>      second child of second
>    </div>
>  </div>
>  <div class="second">
>    second
>    first child of second
>    first grandchild of second
>    <div class="first-child-of-second">
>      first child of second
>      first grandchild of second
>      <div class="first-grandchild-of-second">
>        first grandchild of second
>      </div>
>    </div>
>  </div>
>  <div class="third">
>    third
>  </div>
>
> How can I avoid this?
>
> sdt

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-2011 All Rights Reserved.