[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Flattening hierarchic xml, retaining some structure

Subject: Re: [xsl] Flattening hierarchic xml, retaining some structure
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Wed, 19 Jan 2005 18:30:28 +0100

Tempore 15:56:08, die 01/19/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Ragulf Pickaxe <jawxml@xxxxxxxxxxx>:

I have a hierarchic structure, that I would like to flatten.
I have tried looking for some examples, but I have not been able to expand the examples I have found, so that they will work on my problem.

The followng stylesheet will do the job when applied to the sample XML. It most likely needs some modification to fit the real XML.

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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="mynode">
<xsl:apply-templates select="*[not(child::mynode)][position()=1 or preceding-sibling::*[1]/child::mynode]" mode="encapsule"/>
<xsl:apply-templates select="../following::*[1]/mynode"/>

<xsl:template match="mynode/*" mode="encapsule">
		<xsl:copy-of select="../@*"/>
		<xsl:apply-templates select="." mode="list"/>
	<xsl:apply-templates select="(following-sibling::*/mynode)[1]"/>

<xsl:template match="mynode/*" mode="list">
<xsl:copy-of select="."/>
<xsl:apply-templates select="following-sibling::*[1][not(child::mynode)]" mode="list"/>


regards, -- Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041) "Et ipsa scientia potestas est" - Francis Bacon , Meditationes sacrae

Current Thread