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

Re: Build hierarchies

Subject: Re: Build hierarchies
From: Ivan Churkin <_qq_@xxxxxx>
Date: Wed, 26 Nov 2003 20:11:46 +0600
churkin
One pure XSLT solution of this problem

Input XML
-----------------------------------------------------------------------------
<root>
  <para role="h1">one</para>
  <para>one_1</para>
  <para>one_2</para>
  .............................
  <para role="h1">two</para>
  <para>two_1</para>
  <para>two_2</para>
  ........................
</root>

XSLT
-----------------------------------------------------------------------------
<?xml version="1.0"?>
<t:transform version="1.0" xmlns:t="http://www.w3.org/1999/XSL/Transform">
  
  <t:template match="/root">
    <root><t:apply-templates select="(para[@role])[1]"  mode="r"/></root>
  </t:template>
  
  <t:template match="para[@role]" mode="r">
    <para><t:copy-of select="@role"/>
      <text><t:value-of select="."/></text>
      <t:apply-templates select="(following-sibling::node())[1]" mode="c"/>
    </para>
    <t:apply-templates select="(following-sibling::para[@role])[1]" mode="r"/>
  </t:template>
  
  <t:template match="node()" mode="c">
    <t:copy-of select="."/>
    <t:apply-templates select="(following-sibling::node())[1]" mode="c"/>
  </t:template>
  
  <t:template match="para[@role]" mode="c" priority="1"/>

</t:transform>

Output
------------------------------------------------------------------------------
<root>
- <para role="h1">
  <text>one</text> 
  <para>one_1</para> 
  <para>one_2</para>
  ....................
  </para>
- <para role="h1">
  <text>two</text> 
  <para>two_1</para> 
  <para>two_2</para>
  .................
  </para>
  </root>
------------------------------------------------------------------------------

SL> Does anyone have thoughts on the suitability of xslt/techniques for
SL> transforming non-nested structures to nested ones? e.g.
SL> Given input such as:

SL> <para role="h1">Text</para>
SL> <para/>
SL> <para/>
SL> {deleted stuff...}
SL> <para role="h1">
SL> ....

SL> produce output such as:

SL> <para role="h1"><para>Text</para>
SL> <para/>
SL> <para/>
SL> {deleted stuff...}
SL> </para>

SL> ...where the appearance of the second <para role="h1"> effectively marks 
SL> the closure of the preceeding one.

-- 
Best regards,
 Ivan Churkin                            mailto:_qq_@xxxxxx



 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.