[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: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 03 Feb 2006 10:56:24 -0500
template type conversion
Sebastian,

In addition to what David said, consider the following more succinct version of your algorithm:

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

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

It does the same thing the same way. I often encourage newcomers to try writing things the long way, as it makes things more explicit. Here, the difference between the long way and the short way (using literal result elements and an attribute value template instead of xslt instructions for everything) is so dramatic, I thought it worthwhile making the opposite recommendation.

In particular (in the context of the question you first raised), note that no explicit statement requesting any values (i.e., any data content) is here, or called for -- look ma, no value-of -- in either this version or yours (as corrected by DC). That's because it's using, instead, the built-in template for text nodes, which looks like this, and which will fire, since you have nothing to override it:

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

Trusting that this will work and getting the hang of "letting it happen" in XSLT's template-matching processing model is often an early hurdle, higher for some learners than others.

Cheers,
Wendell

At 07:12 AM 2/3/2006, you wrote:
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>


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

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.