473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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>

Jul 20 '05 #1
4 1748
> 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
Jul 20 '05 #2
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


Jul 20 '05 #3
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


Jul 20 '05 #4
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

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BT3 | last post by:
I am trying to replace a single record in a flat file. The file is relatively small and no need for database. I open the file, and save the ftell() value in a variable. I read a record using...
19
by: dcrespo | last post by:
Hi all... Is there a way to print a PDF file directly from Python without having Acrobat installed? I know about ReportLab. It's a python module that lets you create almost any PDF document, but I...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
12
by: J. G. | last post by:
I'm looking at rewriting some stand-alone Pro*COBOL applications that read flat files and spit out some reports. Is there any way to mimic COBOL's ability to read lines from a flat file into a...
4
by: Ben | last post by:
So, at my place of employment, we use a national standard to transmit data between certain applications. This standard consists of a fixed width, flat file 4500-some-odd chars wide that contain...
0
by: mrkbrndck | last post by:
I would like to open a log file in a windows UI that corresponds to the file maintained in the loggingDistributorConfig.config file. Has any one done this? Below is the code I tried to use and...
8
by: asrs63 | last post by:
Hi, I am a newbee and have a comma seperated flat-file and a DTD. I need to write a C# program which will read the text file and convert it to a XML file as per the the definition in the DTD. I...
3
by: CrazyAtlantaGuy | last post by:
I am working on creating an XSLT that transforms Html into an XML format that can be imported into Framemaker. The challenge, it turns out, is correctly transforming the flat html header tags...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.