Re: [xsl] Build hierarchies

Subject: Re: [xsl] Build hierarchies
From: Ivan Churkin <_qq_@xxxxxx>
Date: Wed, 26 Nov 2003 20:11:46 +0600
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