From a flat XML structure to a hierachical format with XSL

Discussion in 'XML' started by charles, Dec 6, 2004.

  1. charles

    charles Guest

    Using XSL, how can I go from this format:

    <T4>
    <T4_slip>1</T4_slip>
    <T4_slip>2</T4_slip>
    <T4_slip>3</T4_slip>
    <T4_slip>4</T4_slip>
    <T4_summary>20</T4_summary>
    <T4_slip>1</T4_slip>
    <T4_slip>2</T4_slip>
    <T4_slip>3</T4_slip>
    <T4_slip>4</T4_slip>
    <T4_summary>20</T4_summary>
    </T4>

    to this format:
    <T4>
    <group>
    <T4_slip>1</T4_slip>
    <T4_slip>2</T4_slip>
    <T4_slip>3</T4_slip>
    <T4_slip>4</T4_slip>
    <T4_summary>20</T4_summary>
    </group>
    <group>
    <T4_slip>1</T4_slip>
    <T4_slip>2</T4_slip>
    <T4_slip>3</T4_slip>
    <T4_slip>4</T4_slip>
    <T4_summary>20</T4_summary>
    </group>
    </T4>
    charles, Dec 6, 2004
    #1
    1. Advertising

  2. charles

    Joris Gillis Guest

    > Using XSL, how can I go from this format:
    >
    > <T4>
    > <T4_slip>1</T4_slip>
    > <T4_slip>2</T4_slip>
    > <T4_slip>3</T4_slip>
    > <T4_slip>4</T4_slip>
    > <T4_summary>20</T4_summary>
    > <T4_slip>1</T4_slip>
    > <T4_slip>2</T4_slip>
    > <T4_slip>3</T4_slip>
    > <T4_slip>4</T4_slip>
    > <T4_summary>20</T4_summary>
    > </T4>
    >
    > to this format:
    > <T4>
    > <group>
    > <T4_slip>1</T4_slip>
    > <T4_slip>2</T4_slip>
    > <T4_slip>3</T4_slip>
    > <T4_slip>4</T4_slip>
    > <T4_summary>20</T4_summary>
    > </group>
    > <group>
    > <T4_slip>1</T4_slip>
    > <T4_slip>2</T4_slip>
    > <T4_slip>3</T4_slip>
    > <T4_slip>4</T4_slip>
    > <T4_summary>20</T4_summary>
    > </group>
    > </T4>
    >
    >


    In XSLT2.0 , that would be quite easy. (http://www.w3.org/TR/xslt20/#grouping)
    In XSLT1.0 , you have to be more creative. Here's one approach:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:eek:utput indent="yes" method="xml"/>

    <xsl:template match="T4">
    <T4>
    <xsl:apply-templates select="T4_summary" mode="group"/>
    </T4>
    </xsl:template>


    <xsl:template match="T4_summary" mode="group">
    <group>
    <xsl:apply-templates select="preceding::T4_slip[generate-id(following::T4_summary[1]) = generate-id(current())]"/>
    <xsl:apply-templates select="."/>
    </group>
    </xsl:template>

    <xsl:template match="T4/*">
    <xsl:copy-of select="."/>
    </xsl:template>

    </xsl:stylesheet>

    regards,

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
    Joris Gillis, Dec 6, 2004
    #2
    1. Advertising

  3. charles

    charles Guest

    Thanks, it works great, very effective.

    Charles.

    Joris Gillis wrote:
    > > Using XSL, how can I go from this format:
    > >
    > > <T4>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > </T4>
    > >
    > > to this format:
    > > <T4>
    > > <group>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > </group>
    > > <group>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > </group>
    > > </T4>
    > >
    > >

    >
    > In XSLT2.0 , that would be quite easy.

    (http://www.w3.org/TR/xslt20/#grouping)
    > In XSLT1.0 , you have to be more creative. Here's one approach:
    >
    > <?xml version="1.0" encoding="utf-8"?>
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    version="1.0">
    >
    > <xsl:eek:utput indent="yes" method="xml"/>
    >
    > <xsl:template match="T4">
    > <T4>
    > <xsl:apply-templates select="T4_summary" mode="group"/>
    > </T4>
    > </xsl:template>
    >
    >
    > <xsl:template match="T4_summary" mode="group">
    > <group>
    > <xsl:apply-templates

    select="preceding::T4_slip[generate-id(following::T4_summary[1]) =
    generate-id(current())]"/>
    > <xsl:apply-templates select="."/>
    > </group>
    > </xsl:template>
    >
    > <xsl:template match="T4/*">
    > <xsl:copy-of select="."/>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > regards,
    >
    > --
    > Joris Gillis

    (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    > Ceterum censeo XML omnibus esse utendum
    charles, Dec 6, 2004
    #3
  4. charles

    charles Guest

    Thanks, it works great, very effective.

    Charles.

    Joris Gillis wrote:
    > > Using XSL, how can I go from this format:
    > >
    > > <T4>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > </T4>
    > >
    > > to this format:
    > > <T4>
    > > <group>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > </group>
    > > <group>
    > > <T4_slip>1</T4_slip>
    > > <T4_slip>2</T4_slip>
    > > <T4_slip>3</T4_slip>
    > > <T4_slip>4</T4_slip>
    > > <T4_summary>20</T4_summary>
    > > </group>
    > > </T4>
    > >
    > >

    >
    > In XSLT2.0 , that would be quite easy.

    (http://www.w3.org/TR/xslt20/#grouping)
    > In XSLT1.0 , you have to be more creative. Here's one approach:
    >
    > <?xml version="1.0" encoding="utf-8"?>
    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    version="1.0">
    >
    > <xsl:eek:utput indent="yes" method="xml"/>
    >
    > <xsl:template match="T4">
    > <T4>
    > <xsl:apply-templates select="T4_summary" mode="group"/>
    > </T4>
    > </xsl:template>
    >
    >
    > <xsl:template match="T4_summary" mode="group">
    > <group>
    > <xsl:apply-templates

    select="preceding::T4_slip[generate-id(following::T4_summary[1]) =
    generate-id(current())]"/>
    > <xsl:apply-templates select="."/>
    > </group>
    > </xsl:template>
    >
    > <xsl:template match="T4/*">
    > <xsl:copy-of select="."/>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > regards,
    >
    > --
    > Joris Gillis

    (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    > Ceterum censeo XML omnibus esse utendum
    charles, Dec 6, 2004
    #4
  5. hello charles,

    a quite generic solution
    adds elements with an explicit
    depth-attribute to the flat
    xml-structure, like that:

    > <group depth="2" />
    > <T4_slip>1</T4_slip>
    > <T4_slip>2</T4_slip>
    > <T4_summary>20</T4_summary>


    > <group depth="2" />
    > <T4_slip>1</T4_slip>
    > <T4_slip>2</T4_slip>
    > <T4_summary>20</T4_summary>


    provided the rule, that elements without
    any depth or elements with a depth greater
    than the depth of the preceeding element
    are nested accordingly, you can build a
    ddeply nested xml structure just from
    the depth information. This works also
    recursively. for details see

    http://www.jbusse.de/xslt/pwf2xml-doc.html

    johannes
    Johannes Busse, Dec 7, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    3,591
    A. Bolmarcich
    May 27, 2005
  2. Ksenia Marasanova

    convert flat structure into hierarchical one

    Ksenia Marasanova, Sep 26, 2004, in forum: Python
    Replies:
    3
    Views:
    543
    Ksenia Marasanova
    Sep 27, 2004
  3. Patrick Jox

    Hierachical Directory Structure

    Patrick Jox, Aug 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    289
    Patrick Jox
    Aug 14, 2006
  4. JJ
    Replies:
    7
    Views:
    317
  5. JJ
    Replies:
    4
    Views:
    302
Loading...

Share This Page