xsl-list
[Top] [All Lists]

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

2008-06-16 02:53:42
Hi Andrew...

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 output is as below:

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>

Thanks & Regards
Mandar

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>
--~--

Please do not print this email unless it is absolutely necessary. 

The information contained in this electronic message and any attachments to 
this message are intended for the exclusive use of the addressee(s) and may 
contain proprietary, confidential or privileged information. If you are not the 
intended recipient, you should not disseminate, distribute or copy this e-mail. 
Please notify the sender immediately and destroy all copies of this message and 
any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should 
check this email and any attachments for the presence of viruses. The company 
accepts no liability for any damage caused by any virus transmitted by this 
email. 

www.wipro.com

--~------------------------------------------------------------------
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>