[xsl] Re: Flattening parts of a document heirarchy

Subject: [xsl] Re: Flattening parts of a document heirarchy
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Fri, 31 Oct 2003 14:02:17 +0100
This transformation:

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

 <xsl:strip-space elements="*"/>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/*">
   <xsl:copy>
     <xsl:copy-of select="@* | node()[not(self::sec)]"/>
    <xsl:for-each select=".//sec">
      <section id="s{position()}"
               level="{count(ancestor::sec)+1}">
        <xsl:copy-of select="node()[not(self::sec)]"/>
      </section>
    </xsl:for-each>
   </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<doc>
      text text text
  <sec id="sec1">
    <p>text1text1text1</p>
    <sec id="sec1.1">
      <p>text2 text2 text2</p>
    </sec>
    <p>text3 text3 text3</p>
  </sec>
</doc>

produces the correct resulting flattenned document:

<doc>
      text text text
  <section id="s1" level="1">
      <p>text1text1text1</p>
      <p>text3 text3 text3</p>
   </section>
   <section id="s2" level="2">
      <p>text2 text2 text2</p>
   </section>
</doc>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL



"David Holden" <dh@xxxxxxxx> wrote in message
news:200310311122.16235.dh@xxxxxxxxxxx
> On Friday 17 Oct 2003 2:10 pm, David Holden wrote:
> > I know there are faq's on flattening but as  newbie i'm  a little
unclear
> > on this.
> >
> > My problem:
> >
> > incoming document is something like this:
> >
> > <doc>
> >     text text text
> >     <sec id="sec1">
> >        <p>text1text1text1</p>
> >         <sec id="sec1.1">
> >            <p>text2 text2 text2</p>
> >         </sec>
> >        <p>text3 text3 text3</p>
> >    </sec>
> > </doc>
> >
> > i.e <sec> tags are nested to arbitary level.
> >
> > I need
> >
> > <doc>
> >    text text text
> >    <section id="s1" level="1">
> >        <p>text1 text1 text1</p>
> >    </section>
> >    <section id="s2" level="2">
> >         <p>text text text</p>
> >    </section>
> >    <section id="s3" level="1">
> >        <p>text3 text3 text3</p>
> >    </section>
> > </doc>
> >
> >
> >  thanks in advance,
> >
> >  Dave.
>
> No replies to this, can XSLT really not do this? I've decided to address
this
> by pre-filtering using SAX where this kind of transform is pretty easy.
>
>  Dave.
>
>
> -- 
> Dr. David Holden. (Systems Developer)
> Crystallography Journals Online: <http://journals.iucr.org>
>
> Thanks in advance:-
> Please avoid sending me Word or PowerPoint attachments.
> See: <http://www.fsf.org/philosophy/no-word-attachments.html>
>
> UK Privacy (R.I.P)  : http://www.stand.org.uk/commentary.php3
> Public GPG key available on request.
> -------------------------------------------------------------
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread