From a flat XML structure to a hierachical format with XSL

C

charles

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

Joris Gillis

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,
 
C

charles

Thanks, it works great, very effective.

Charles.

Joris said:
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())]"/>
 
C

charles

Thanks, it works great, very effective.

Charles.

Joris said:
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())]"/>
 
J

Johannes Busse

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top