xsl-list
[Top] [All Lists]

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

2008-06-12 02:26:33
2008/6/12 Mandar Jagtap <mandar(_dot_)jagtap(_at_)gmail(_dot_)com>:
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/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>