Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   From a flat XML structure to a hierachical format with XSL (http://www.velocityreviews.com/forums/t168373-from-a-flat-xml-structure-to-a-hierachical-format-with-xsl.html)

charles 12-06-2004 03:47 PM

From a flat XML structure to a hierachical format with XSL
 
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>


Joris Gillis 12-06-2004 04:24 PM

Re: From a flat XML structure to a hierachical format with XSL
 
> 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:output 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-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum

charles 12-06-2004 07:56 PM

Re: From a flat XML structure to a hierachical format with XSL
 
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:output 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-v...i?userid=38041)
> Ceterum censeo XML omnibus esse utendum



charles 12-06-2004 07:56 PM

Re: From a flat XML structure to a hierachical format with XSL
 
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:output 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-v...i?userid=38041)
> Ceterum censeo XML omnibus esse utendum



Johannes Busse 12-07-2004 08:54 AM

Re: From a flat XML structure to a hierachical format with XSL
 
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



All times are GMT. The time now is 10:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.