RE: Non-hierarchy XML -> hierarchy XML
> [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
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format