TclXSLT question: best approach?



I'm prototyping an app that uses XML config files. This morning I knew just enough XML to ask stupid questions, and even less about XSL. But with a little help from O'Reilly's "XML Hacks" (the Socket Wrench book) and w3.org I learned quite a bit.

My goal was to convert an XML file defining a series of nodes, each with six attributes, into a list of lists, with the attributes in the inner lists in a standard order regardless of their order in the XML. (I was surprised how many quick & dirty solutions just copy things in document order, which seems like asking for trouble!) I created an .xsl file that performed the transform, which I tested by opening the .xml file in a browser. Woo hoo, the transform works!

Then I used tclXSLT to do it in Tcl thusly (where $xml and $xsl are the contents of the respective files):

package require dom
package require xslt
set xmldoc [::dom::libxml2::parse $xml]
set xsldoc [::dom::libxml2::parse $xsl]
set listify [::xslt::compile $xsldoc]
set txtdoc [$listify transform $xmldoc]
set bands [::dom::libxml2::serialize $txtdoc -method text]

That works, so obviously I've found *a* way, but when venturing into unknown territory I like to keep to the *right* way as much as possible.

One question that arose immediately was why do I have to say "-method text" when the XSL already specifies <xsl:output method="text" />? I guess for no other reason than that the man page says I have to, huh?

The other question is one of the whole approach. Having a separate .xsl file makes it easy to view the XML files in a browser, which is potentially useful. But I could have just walked the XML doc tree in Tcl, pulling out attributes and stuffing them in a list. In fact, the code to do this would look a lot like the templates in the .xsl file.

How would the Tcl/XML/XSL graybeards do this?

(I am a mere salt&pepperbeard.)
.



Relevant Pages