XML, or eXtensible Markup Language, is a data format.
XML is a simplified form of SGML, but stricter (more regular) in some aspects:
Example:
<father name="Jack" att1="1"> <child name="Tom" born="1997" /> </father>
Tcl's Unicode features make it a good language for processing XML.
There is broad confusion about whether to represent attributes of things that are the subject of an XML document as attributes of a tag or as the content of individual entities within an XML entity, and misuse abounds. The intent of the specification is clear even if it is not explicit: An attribute in an XML tag describes the entity that is part of the structure of the document, not the thing in the subject of the document the entity refers to. In other words, data pertaining to the subject of the document comprise the content of tags, and data that describe the document itself comprise the attributes of tags. A more concise way to put it is that entities are structure, and content is content.
The confusion may arise partially because the subject of some part of the document may be the document itself, in which case data about the structure and interpretation of the document may occur as content. In this case, data that could occur as attributes in a tag in one part of the document occurs instead as content in another part of the document. This is legitimate. The inverse case, where content occurs as attributes of a tag, is not.
tDOM and TclXML/TclDOM are the two main Tcl extensions for parsing XML, providing both SAX parsing for stream-oriented parsing, and DOM for document-oriented parsing.
See Also:
[Regular Expressions Are Not A Good Idea for Parsing XML, HTML, or e-mail Addresses].
One way of specifying the valid tag structure of a class of documents is to use a DTD, or. This way was inherited from SGML. There are alternative ways ... XMLSchema, Relax(NG), ...
See Also:
In a mailing list conversation [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 must be escaped.
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 { < < > > & & \" " ' ' } 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
DKF: That's when you're moving away from cheap hacks. And HTML has a lot more entities than XML, though most are optional.
If you want to get particular about entity encoding arbitrary text, this is working for me:
variable entityMap [list & &\; < <\; > >\; \" "\;\ \u0000
There are a whole host of technologies related to XML:
tDOM and TclXML both provide good support for at least XPath and XSLT.
XML by itself is just a partially-standardized syntax for data. It's used as the basis for a variety of different applications, such as:
Alternatives to using XML for data files include: