RE: [xsl] Non-hierarchy XML -> hierarchy XML

Subject: RE: [xsl] Non-hierarchy XML -> hierarchy XML
From: "Paul R Brown" <prb@xxxxxxxxxxxxx>
Date: Tue, 26 Jun 2001 19:46:49 -0500
> [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:


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

                          ^-----^ remove
                      ^-----^ remove

And you're left with the desired

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:

Current Thread