Re: [xsl] Need to Split/Un-Nest elements

Subject: Re: [xsl] Need to Split/Un-Nest elements
From: "Mandar Jagtap" <mandar.jagtap@xxxxxxxxx>
Date: Tue, 17 Jun 2008 16:16:23 +0530
Hi Andrew,

I am not sure if you got chance to take a look my last post in the
thread so describing the same again as below:

Thanks for the response and stylesheet too...It's working well with me
but with one limitation. In the input xml, I can't be sure about that
how many nested block level elements would be there inside <page> i.e.
descendants of <page> can be many nested <block> elements and also they
can be <list>, <table> etc.  I tried using <xsl:template match="*"
mode='copy'> instead of <xsl:template match="inline" mode="copy"> in the
stylesheet but in that case it doesn't copy parent elements of original
<inline> or <block> from original <page> element to newly generated
(replicated) <page>. i.e. as per example below, it doesn't copy the 3
nested <block> elements in the replicated <page> element. Could you
please suggest on this?

Current Input XML:

<?xml version='1.0'?>

<region>
  <page>
       <block>Text1.....</block>
         <block>
               <block>
                       <block>
                               <inline>Text2.....</inline>

                               <table>
                                       <cell>cell1.....</cell>
                                       <cell>cell2.....</cell>
                               </table>

                               <page>
                                       <block>Text3.....</block>
                               </page>

                               <list>
                                       <item1>Text4......</item1>
                                       <item2>Text5......</item2>
                               </list>
                       </block>
               </block>

         </block>
  </page>
  <page>
       <block>Text5.....</block>
  </page>
</region>



Current Output using <xsl:template match="*" mode="copy">:

<?xml version="1.0" encoding="UTF-8"?>

<region>
  <page>
       <block>Text1.....</block>
       <block>
             <block>
                       <block>
                             <inline>Text2.....</inline>

                             <table>
                               <cell>cell1.....</cell>
                               <cell>cell2.....</cell>
                             </table>
                         </block>
               </block>

       </block>
  </page>
  <page>
       <block>Text3.....</block>
  </page>
  <page>

     <list>
          <item1>Text4......</item1>
          <item2>Text5......</item2>
       </list>

  </page>
  <page>
       <block>Text5.....</block>
  </page>
</region>


Stylesheet:

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

   <xsl:output indent="yes"/>

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

   <xsl:template match="@*">
       <xsl:copy/>
   </xsl:template>

   <xsl:template match="/region">
       <xsl:copy>
           <xsl:apply-templates
select="//page|//*[preceding-sibling::*[1][self::page]]" mode="copy"/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="page" mode="copy">
       <xsl:copy>
           <xsl:apply-templates select="node()[1]"/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="*" mode="copy">
       <page>
            <xsl:apply-templates select="."/>
       </page>
   </xsl:template>

   <xsl:template match="page"/>

</xsl:stylesheet>

Thanks & Regards
Mandar

On 6/12/08, Andrew Welch <andrew.j.welch@xxxxxxxxx> wrote:
> 2008/6/12 Mandar Jagtap <mandar.jagtap@xxxxxxxxx>:
> > Hi,
> >
> > I want to split/un-nest the elements in xml like below:
> >
> > Input xml:
> >
> >         <region>
> >            <page>
> >               <block>Text1.....</block>
> >               <block>
> >                    <inline>Text2.....</inline>
> >                    <page>
> >                        <block>Text3.....</block>
> >                    </page>
> >                    <inline>Text4......</inline>
> >               </block>
> >            </page>
> >            <page>
> >               <block>Text5.....</block>
> >            </page>
> >         </region>
> >
> > Desired Output:
> >
> >   <region>
> >        <page>
> >               <block>Text1.....</block>
> >               <block>
> >                     <inline>Text2.....</inline>
> >               </block>
> >        </page>
> >        <page>
> >               <block>Text3.....</block>
> >        </page>
> >        <page>
> >               <block>
> >                   <inline>Text4......</inline>
> >               </block>
> >        </page>
> >        <page>
> >               <block>Text5.....</block>
> >        </page>
> >
> >   </region>
> >
> > Can anyone help me on this?
>
>
> You need the "modified identity" or "sibling recursion" technique to
> allow you stop processing as you come across a <page> element:
>
>
> <xsl:stylesheet version="1.0"
>    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
>    <xsl:output indent="yes"/>
>
>    <xsl:template match="node()">
>        <xsl:copy>
>            <xsl:apply-templates select="@*|node()[1]"/>
>        </xsl:copy>
>        <xsl:apply-templates select="following-sibling::node()[1]"/>
>    </xsl:template>
>
>    <xsl:template match="@*">
>        <xsl:copy/>
>    </xsl:template>
>
>    <xsl:template match="/region">
>        <xsl:copy>
>            <xsl:apply-templates
> select="//page|//*[preceding-sibling::*[1][self::page]]" mode="copy"/>
>        </xsl:copy>
>    </xsl:template>
>
>    <xsl:template match="page" mode="copy">
>        <xsl:copy>
>            <xsl:apply-templates select="node()[1]"/>
>        </xsl:copy>
>    </xsl:template>
>
>    <xsl:template match="inline" mode="copy">
>        <page>
>            <block>
>                <xsl:apply-templates select="."/>
>            </block>
>        </page>
>    </xsl:template>
>
>    <xsl:template match="page"/>
>
> </xsl:stylesheet>
>
>
> Sorry I don't have time to explain it...
>
>
> --
> Andrew Welch
> http://andrewjwelch.com
> Kernow: http://kernowforsaxon.sf.net/
>
>


-- 
Thanks & Regards,
Mandar

Current Thread