Tools
The tools/ directory contains a few helper scripts used during
development. The three relevant ones are described here.
xmltotdom.tcl
Reads an XML file and generates Tcl source code that rebuilds the same XML using the classic tdom DOM API (createElement, setAttribute, appendChild, ...).
Using this sample input demo.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Demo>
<!--comment block goes here-->
<Node attribute="value">
<SubNode>text data</SubNode>
</Node>
<?piTag the processing instructions goes here?>
<Node><![CDATA[</this is malformed!</malformed</malformed & worse cDATA>]]></Node>
</Demo>
it generates:
package require tdom 0.9.0-
set encoding utf-8
set doc [dom createDocument Demo]
set root [$doc documentElement]
$root appendChild [$doc createComment {comment block goes here}]
$root appendChild [set node0 [$doc createElement Node]]
$node0 setAttribute attribute value
$node0 appendChild [set node1 [$doc createElement SubNode]]
$node1 appendChild [$doc createTextNode {text data}]
$root appendChild [$doc createProcessingInstruction piTag {the processing instructions goes here}]
$root appendChild [set node0 [$doc createElement Node]]
$node0 appendChild [$doc createCDATASection {</this is malformed!</malformed</malformed & worse cDATA>}]
fconfigure stdout -encoding $encoding
puts [$root asXML -indent 2 -xmlDeclaration 1 -encString [string toupper $encoding]]
xmltotdomnodecmd.tcl
Like xmltotdom.tcl, but generates code that uses the tdom nodecmd API
(createNodeCmd / appendFromScript). For the same demo.xml it generates:
package require tdom 0.9.0-
set encoding utf-8
dom createNodeCmd cdataNode CData
dom createNodeCmd commentNode Comment
dom createNodeCmd piNode PInstr
dom createNodeCmd textNode Text
dom createNodeCmd -tagName Node elementNode Tag_Node
dom createNodeCmd -tagName SubNode elementNode Tag_SubNode
set doc [dom createDocument Demo]
set root [$doc documentElement]
$root appendFromScript {
Comment {comment block goes here}
Tag_Node attribute value {
Tag_SubNode {
Text text data
}
}
PInstr piTag {the processing instructions goes here}
Tag_Node {
CData {</this is malformed!</malformed</malformed & worse cDATA>}
}
}
fconfigure stdout -encoding $encoding
puts [$root asXML -indent 2 -xmlDeclaration 1 -encString [string toupper $encoding]]
createTypeCode.tcl
Reads a simple XSD enumeration type definition from stdin and generates a Tcl
type check proc for the ::ooxml::docx::lib namespace from it. It is used to
turn the enumeration types of the WordprocessingML schema into the value
checking procedures of the docx writer. It is intended to be called from
Emacs via shell-command-on-region (M-|) with the XSD type definition as the
region.
Given this XSD simpleType on stdin:
<xsd:simpleType name="ST_Border">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="single"/>
<xsd:enumeration value="thick"/>
<xsd:enumeration value="double"/>
</xsd:restriction>
</xsd:simpleType>
it generates:
proc ::ooxml::docx::lib::ST_Border {value} {
set values {
single
thick
double
}
if {$value in $values} {
return $value
}
error "unknown ST_Border value \"$value\", expected one of\
[AllowedValues $values]"
}