[xsl] Building up a hierarchy from a flat structure

Subject: [xsl] Building up a hierarchy from a flat structure
From: "Wolfgang Schindler" <w.schindler@xxxxxxxxxxxxx>
Date: Wed, 26 Nov 2003 10:02:51 +0100
Dear colleagues,

I have an XML file with a set of entries, each containing a sequence of elements. I have to introduce container elements that group some of these elements together. The A element is intended as a hook where I want to start grouping all the following-siblings so that all following-siblings after a specific A are grouped together as children of A until the next A or the end of the ENTRY is reached. One problem is that I want to keep the sequence of elements as in the original and do not know what elements in which frequency and in what sequence occur in my data, the second problem is that I don't have the A hook at the end of the ENTRY. How would such a procedural logics (start at a certain element to move each following element in turn into a container element until a certain element is reached) be implemented in XSLT?

The sample files have been simplified. In the original the elements are not empty, but contain substructure.

See in.xml (the input) and out.xml(the intended output).

IN.XML: Input file

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
	<ENTRY>
		<X/>
		<A/>
		<SG/>
		<TG/>
		<TG/>
		<U/>
		<U/>
		<U/>
		<TG/>
		<A/>
		<SG/>
		<TG/>
	</ENTRY>
	<ENTRY>
		<B/>
		<A/>
		<SG/>
		<TG/>
		<SG/>
		<TG/>
		<TG/>
		<TG/>
		<SG/>
		<TG/>
		<A/>
		<SG/>
		<TG/>
	</ENTRY>
	<ENTRY>
		<C/>
		<A/>
		<SG/>
		<U/>
		<SG/>
		<TG/>
		<A/>
		<SG/>
		<U/>
		<U/>
		<U/>
		<U/>
		<U/>
	</ENTRY>
	<ENTRY>
		<Y/>
		<A/>
		<SG/>
		<U/>
		<TG/>
		<U/>
		<TG/>
		<A/>
		<SG/>
		<TG/>
		<TG/>
		<TG/>
	</ENTRY>
	<ENTRY>
		<C/>
		<Z/>
	</ENTRY>
</ROOT>

OUT.XML: Desired Output

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
	<ENTRY>
		<X/>
		<A>
			<SG/>
			<TG/>
			<TG/>
			<U/>
			<U/>
			<U/>
			<TG/>
		</A>
		<A>
			<SG/>
			<TG/>
		</A>
	</ENTRY>
	<ENTRY>
		<B/>
		<A>
			<SG/>
			<TG/>
			<SG/>
			<TG/>
			<TG/>
			<TG/>
			<SG/>
			<TG/>
		</A>
		<A>
			<SG/>
			<TG/>
		</A>
	</ENTRY>
	<ENTRY>
		<C/>
		<A>
			<SG/>
			<U/>
			<SG/>
			<TG/>
		</A>
		<A>
			<SG/>
			<U/>
			<U/>
			<U/>
			<U/>
			<U/>
		</A>
	</ENTRY>
	<ENTRY>
		<Y/>
		<A>
			<SG/>
			<U/>
			<TG/>
			<U/>
			<TG/>
		</A>
		<A>
			<SG/>
			<TG/>
			<TG/>
			<TG/>
		</A>
	</ENTRY>
	<ENTRY>
		<C/>
		<Z/>
	</ENTRY>
</ROOT>

Many thanks in advance for your patience and your kind assistance!

Best regards
Wolfgang


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread