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

removing nesting in data structure

Subject: removing nesting in data structure
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Mon, 20 May 2002 12:09:31 +0100
xsl 2 passes
In my ongoing quest to find a way around the problems I am having with
nested data and css, I have decided to try out the two pass solution.  I
know that if my data is held in a certain way, then I will have no
problems when it comes to converting it to html. 

So, the data is nested like this:

<?xml version="1.0"?>
<root>
  <para0>
    <para>para 0</para>
    <subpara1>
      <para>subpara 1</para>
      <subpara2>
        <para>subpara 2</para>
        <subpara3>
          <para>subpara 3</para>
        </subpara3>
      </subpara2>
    </subpara1>
  </para0>
</root>

Each subpara is a child of the preceding subpara, up to para0 which is
the top level. The nested element will always be the last child of the
parent.  Now the structure I would like is more flat, like this:

<root>
  <para0>
    <para>para 0</para>
  </para0>
  <subpara1>
    <para>subpara1</para>
  </subpara1>
  <subpara2>
    <para>subpara2</para>
  </subpara2>
  ...and so on
</root>

Ive written this template, that seems to do the job:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                version="1.0">

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


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


<xsl:template match="para0|subpara1|subpara2|subpara3">
  <xsl:copy>
    <xsl:apply-templates
select="*[not(subpara1)][not(subpara2)][not(subpara3)]"/>
  </xsl:copy>
  <xsl:apply-templates select="subpara1|subpara2|subpara3"/>
</xsl:template>

</xsl:stylesheet>


Can anyone comment on this, is it the best way?  Is there an Xpath I
could use on the nested data that would select the data in this way,
avoiding the need for 2 passes?  Im pretty sure there isnt, but Im
constantly surprised by the new uses of xslt...

cheers
andrew

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.350 / Virus Database: 196 - Release Date: 17/04/2002
 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.