[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Non-hierarchy XML -> hierarchy XML

Subject: RE: Non-hierarchy XML -> hierarchy XML
From: "Paul R Brown" <prb@xxxxxxxxxxxxx>
Date: Tue, 26 Jun 2001 19:46:49 -0500
paul r brown
> [Benoit Aumars]
> I would like to transform a node into a hierarchy node
> using a pattern, i.e <node> into <AAA><BBB><CCC>.

First, you have to adhere to the four-line sig rule...  Second, I may need
some glasses, but I don't see a way to accomplish your intent with XSLT,
since it requires side effects.  (The portion of the document generated by
node 1 in the sequence node1, node 2 is different than that generated in
node1, node 3.)

> Any suggestion how can I achieve that transformation ?

You need a tree folding algorithm.  (Try "Topology of Finite Graphs",
Stallings, Annals of Mathematics for a nice, classical overview of folding
of labeled trees...)  Here are some hints for your exercise:

1) Use the XSLT method suggested by Michael Kay to generate a tree that
looks like:

<base>
	<a>
		<b>
			<c>foo</c>
		</b>
	</a>
	<a>
		<b>
			<d>bar</d>
		</b>
	</a>
</base>

Your XSLT script will have a template for each of the elements in your list.

2) Implement the tree folding algorithm, and because of XML notation, there
is no better way to do that than in Perl... :)  The rest of your exercise is
to turn the following into Perl code:

"while the document contains a match for something like </tag><tag>, replace
</tag><tag> with the empty string"

To see why this works, look at

<base><a><b><c>foo</c></b></a><a><b><d>bar</d></b></a></base>
                          ^-----^ remove
<base><a><b><c>foo</c></b><b><d>bar</d></b></a></base>
                      ^-----^ remove

And you're left with the desired
<base><a><b><c>foo</c><d>bar</d></b></a></base>.

Handling attributes in the way you desire is an extra credit problem.

	-- Paul

PS The funny thing is that I actually gave this as an exercise in a graduate
course I taught on Java and XML in 1999...


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.