Version 61 of XML

Updated 2007-12-27 01:21:31 by dkf

XML = eXtensible Markup Language [L1 ]. Very generally spoken it is a simplified form of SGML, but stricter (more regular) in some aspects:

  • Singleton elements must end with />
  • attribute values must be quoted

Example:

 <father name="Jack" att1="1">
   <child name="Tom" born="1997" />
 </father>

"Programming XML in Tcl" [L2 ] surveys the state-of-the-art as of spring 2001, mainly from a Zveno-biased perspective.

One deficiency of that article is its neglect of Jochen Loewer's tDOM work.


One way of specifying the valid tag structure of a class of documents is to use a Document Type Definition, DTD for short. This way was inherited from SGML. There are alternative ways ... XMLSchema, Relax(NG), ...


Perhaps the single most important introductory point to make to Tcl developers about XML is that it's built-in! Almost--while the core Tcl distribution doesn't know about XML, it does have excellent Unicode abilities, and both the ActiveTcl and Kitten installations of Tcl include XML packages.


tDOM builds-in a pretty-printing serialization option. Those with an interest in a comparable function for TclDOM are welcome to try/use/improve/... dom_pretty_print [L3 ]. "XML pretty-printing" will eventually have more on this topic.


How can you start to generate your own XML documents with Tcl? In answering just that question in a mailing list [reference?], Steve Ball succinctly advised, "When creating XML, I generally use TclDOM. Create a DOM tree in memory, and then use 'dom::DOMImplementation serialize $doc' to generate the XML. The TclDOM package will make sure that the generated XML is well-formed.

Alternatively, XML is just text so there's no reason why you can't just create the string directly. Eg:

        puts <document>$content</document>"

The problem with this is that (a) you have to worry about the XML syntax nitty-gritty and (b) the content variable may contain special characters which you have to deal with.

There are also some generation packages available, like the 'html' package in tcllib (this will be added to TclXML RSN, when my workload permits)."

DKF - If you're going for the cheap-hack method of XML generation mentioned above, you'll want this:

  proc asXML {content {tag document}} {
     set XML_MAP {
        < &lt;
        > &gt;
        & &amp;
        \" &quot;
        ' &apos;
     }
     return <$tag>[string map $XML_MAP $content]</$tag>
  }

Naturally, the XML_MAP variable is factorisable... MHo: Why not using html::quoteFormValue for this purpose?

For generation of XML (HTML) the pure Tcl way, have a look at the xmlgen module of TclXML on sourceforge: http://sourceforge.net/projects/tclxml/ .


If you want to get peticular about entity encoding arbitrary text, this is working for me:

 variable entityMap [list & &amp\; < &lt\; > &gt\; \" &quot\;\
        \u0000