expat(3tcl)
π¦ expat π¦
ok so real talk, this is the expat(3tcl) page β like, the actual manual page for expat from the Tcl world. bestie, grab a strawberry π and let's get into it.
β¨ NAME β¨
expat β creates an instance of an expat parser object. that's it, that's the whole vibe. π
β¨ SYNOPSIS β¨
package require tdom
expat ?parsername? ?-namespace? ?arg arg ..
xml::parser ?parsername? ?-namespace? ?arg arg ..
β¨ DESCRIPTION β¨
okay so, the parser you get from expat or xml::parser (which is literally just another name for the exact same command, living in its own namespace β same girl, different outfit π) can parse any kind of well-formed XML. no cap.
these parsers are stream oriented XML parsers. what that means is: you register handler scripts with the parser before you start the parse. then those handler scripts get called whenever the parser stumbles across the associated structures in the document it's parsing. a start tag is, like, the classic example of a structure you'd register a handler script for. πΈ
the parsers always check the input for XML well-formedness β and yes, they report an error if the input isn't well-formed, they are not letting that slide. they parse the internal DTD and, on request, the external DTD and external entities β but only if you resolve the identifier of those external entities with the -externalentitycommand script (see below, iykyk). and if you use the -validateCmd option (also see below), then the input gets additionally validated. extra credit fr fr.
additionally (yes the original spells it "Additionly", iconic typo, we move), the Tcl extension code that implements this command provides an API for adding C level coded handlers. up to now, there's the parser extension command "tdom". the handler set that this extension installs builds an in-memory "tDOM" DOM tree while the parser is parsing the input. main character energy honestly. π
and here's the fun part: it's possible to register an arbitrary amount of different handler scripts and C level handlers for most of the events. when the event happens, they get called in turn. no fighting, everyone gets a turn. π«Ά
β¨ COMMAND OPTIONS β¨
okay bestie, this is the long part. every single one, here we go. π
-namespaceβ enables namespace parsing. β οΈ you must use this option while you're creating the parser with theexpatorxml::parsercommand. you can't enable it (nor disable it) later with<parserobj> configure ....β it's a now-or-never situation, periodt.-namespaceseparatorchar β this one only does anything if you're also using-namespace. if you give it, it decides which character gets inserted between the namespace URI and the local name when reporting an XML element name to a handler script. the default is the character':'. the value has to be a one-character string that's less than or equal to\u00FF, and preferably a 7-bit ASCII character or the empty string. if the value is the empty string (and also if the value is\x00), then the namespace URI and the local name just get concatenated with no separator at all. zero drama. π¦-finalboolean β tells you whether the document data you're about to hand to the parse method is the final part of the document. a value of"0"means more data is expected. a value of"1"means nothing more is coming. the default value is"1".
if this option is set to "0", then the parser will not report certain errors if the XML data isn't well-formed upon end of input β things like unclosed or unbalanced start or end tags. instead, some data may get saved by the parser until the next call to the parse method, which means the reporting of some of the data gets delayed. it's giving "we'll deal with it later" π
if this option is set to "1", then documents that aren't well-formed upon end of input will generate an error. no mercy.
-validateCmd<tdom schema cmd> β this option expects the name of a tDOM schema command. if you give it, then the input gets also validated. and heads up: if the schema command hasn't set areportcmd, then the first validation error will stop further parsing (it gets treated the same as a well-formedness error). π-baseurlurl β reports the base url of the document to the parser. simple, clean, iconic. β¨-fastcallboolean β by default this option is0(off). if you turn it on, then any handler installed from that moment on by the options-elementstartcommand,-elementendcommandand-characterdatacommandgets installed in a way where calling it has less overhead than usual. you can switch this option on and off between callback handler (re-)installations as much as you want, whenever you want. π
β οΈ but there are some requirements, and bestie, these are not optional. the handler proc has to be defined before it's used as a callback. if it isn't, then the callback just gets installed as usual (no speedup, no harm). and the handler proc must not be removed or re-defined for as long as it's being used as a callback. if you do that, the program probably will crash β but it also may execute arbitrary code. yeah. that's a real one. don't do it. π
the callback handler installed while this option is on will not be traced when they're executed.
-keepTextStartboolean β by default this option is0(off). if you turn it on, then the position information of the start of a text or CDATA node gets kept across the collecting of the sometimes-by-expat-delivered cdata pieces. with this option on, the position information returned by the parser in a-characterdatacommandproc will be correct β otherwise, not so much. but listen: called in all other handler code, the parser always returns the correct position results, no matter what value this option has. it's off by default because it's rarely needed and it saves a few cpu cycles this way. efficiency queen. β-elementstartcommandscript β specifies a Tcl command to associate with the start tag of an element. the actual command consists of this option followed by at least two arguments: the element type name and the attribute list.
the attribute list is a Tcl list made of name/value pairs, suitable for passing straight to the array set Tcl command. perfect for it. π
example:
proc HandleStart {name attlist} {
puts stderr "Element start ==> $name has attributes $attlist"
}
$parser configure -elementstartcommand HandleStart
$parser parse {<test id="123"></test>}
this would result in the following command being invoked:
HandleStart text {id 123}
-elementendcommandscript β specifies a Tcl command to associate with the end tag of an element. the actual command consists of this option followed by at least one argument: the element type name. plus, if the-reportemptyoption is set, then the command may be invoked with the-emptyconfiguration option to indicate whether it's an empty element. peep the description of the-reportemptyoption for an example. π
example:
proc HandleEnd {name} {
puts stderr "Element end ==> $name"
}
$parser configure -elementendcommand HandleEnd
$parser parse {<test id="123"></test>}
this would result in the following command being invoked:
HandleEnd test
-characterdatacommandscript β specifies a Tcl command to associate with character data in the document, ie. text. the actual command consists of this option followed by one argument: the text.
and here's the good news: other than with the C API of the expat parser, it is guaranteed that character data will be passed to the application in a single call to this command. no tiny fragment chaos. we love that for us. π
example:
proc HandleText {data} {
puts stderr "Character data ==> $data"
}
$parser configure -characterdatacommand HandleText
$parser parse {<test>this is a test document</test>}
this would result in the following command being invoked:
HandleText {this is a test document}
-processinginstructioncommandscript β specifies a Tcl command to associate with processing instructions in the document. the actual command consists of this option followed by two arguments: the PI target and the PI data.
example:
proc HandlePI {target data} {
puts stderr "Processing instruction ==> $target $data"
}
$parser configure -processinginstructioncommand HandlePI
$parser parse {<test><?special this is a processing instruction?></test>}
this would result in the following command being invoked:
HandlePI special {this is a processing instruction}
-notationdeclcommandscript β specifies a Tcl command to associate with notation declaration in the document. the actual command consists of this option followed by four arguments: the notation name, the base uri of the document (and by that I mean whatever was set by the-baseurloption), the system identifier and the public identifier. the notation name is never empty; the other arguments may be. π€·-externalentitycommandscript β specifies a Tcl command to associate with references to external entities in the document. the actual command consists of this option followed by three arguments: the base uri, the system identifier of the entity and the public identifier of the entity. the base uri and the public identifier may be the empty list.
this handler script has to return a tcl list consisting of three elements. the first element of this list signals how the external entity is returned to the processor. at the moment, the three allowed types are "string", "channel" and "filename". the second element of the list has to be the (absolute) base URI of the external entity to be parsed. the third element of the list is the data β either the already-read data out of the external entity as a string, in the case of type "string"; or the name of a tcl channel, in the case of type "channel"; or the path to the external entity to be read, in the case of type "filename". behind the scene, the external entity referenced by that returned Tcl channel, string or file name will be parsed with an expat external entity parser that has the same handler sets as the main parser. π§
β οΈ if parsing of the external entity fails, the whole parsing is stopped with an error message.
and if a Tcl command registered as externalentitycommand isn't able to resolve an external entity, it's allowed to return TCL_CONTINUE. in that case, the wrapper gives the next registered externalentitycommand a try. and if no externalentitycommand is able to handle the external entity, parsing stops with an error. π₯
example:
proc externalEntityRefHandler {base systemId publicId} {
if {![regexp {^[a-zA-Z]+:/} $systemId]} {
regsub {^[a-zA-Z]+:} $base {} base
set basedir [file dirname $base]
set systemId "[set basedir]/[set systemId]"
} else {
regsub {^[a-zA-Z]+:} $systemId systemId
}
if {[catch {set fd [open $systemId]}]} {
return -code error \
-errorinfo "Failed to open external entity $systemId"
}
return [list channel $systemId $fd]
}
set parser [expat -externalentitycommand externalEntityRefHandler \
-baseurl "file:///local/doc/doc.xml" \
-paramentityparsing notstandalone]
$parser parse {<?xml version='1.0'?>
<!DOCTYPE test SYSTEM "test.dtd">
<test/>}
and bestie, that would result in this exact command being invoked:
externalEntityRefHandler file:///local/doc/doc.xml test.dtd {}
okay so here's the tea β β external entities are only tried to resolve via this handler script if necessary. Like, only when it's actually needed. That means external parameter entities trigger this handler only if -paramentityparsing is used with argument "always", or if -paramentityparsing is used with argument "notstandalone" and the document isn't marked as standalone. β¨
-unknownencodingcommandscript β ngl this one's just not a thing at the Tcl level. Not implemented at Tcl level. π-startnamespacedeclcommandscript β names a Tcl command to associate with the start scope of namespace declarations in the document. The actual command is this option followed by two arguments: the namespace prefix and the namespace URI. For anxmlnsattribute, prefix will be the empty list. For anxmlns=""attribute, uri will be the empty list. And the call to the start and end element handlers occur between the calls to the start and end namespace declaration handlers. π-endnamespacedeclcommandscript β names a Tcl command to associate with the end scope of namespace declarations in the document. The actual command is this option followed by the namespace prefix as argument. In case of anxmlnsattribute, prefix will be the empty list. And again: the call to the start and end element handlers occur between the calls to the start and end namespace declaration handlers. π«Ά-commentcommandscript β names a Tcl command to associate with comments in the document. The actual command is this option followed by one argument: the comment data.
Example, periodt:
proc HandleComment {data} {
puts stderr "Comment ==> $data"
}
$parser configure -commentcommand HandleComment
$parser parse {<test><!-- this is <obviously> a comment --></test>}
and that would result in this command being invoked:
HandleComment { this is <obviously> a comment }
-notstandalonecommandscript β this Tcl command gets called if the document is not standalone (meaning it has an external subset or a reference to a parameter entity, but does not havestandalone="yes"). It's called with no additional arguments. π-startcdatasectioncommandscript β names a Tcl command to associate with the start of a CDATA section. It's called with no additional arguments. π-endcdatasectioncommandscript β names a Tcl command to associate with the end of a CDATA section. It's called with no additional arguments. π¦-elementdeclcommandscript β names a Tcl command to associate with element declarations. The actual command is this option followed by two arguments: the name of the element and the content model. The content model arg is a tcl list of four elements. The first list element specifies the type of the XML element; the six different possible types are reported as"MIXED","NAME","EMPTY","CHOICE","SEQ"or"ANY". The second list element reports the quantifier to the content model in XML Syntax ("?","*"or"+") or is the empty list. If the type is"MIXED", then the quantifier will be"{}", indicating an PCDATA only element, or"*", with the allowed elements to intermix with PCDATA as tcl list as the fourth argument. If the type is"NAME", the name is the third arg; otherwise the third argument is the empty list. If the type is"CHOICE"or"SEQ"the fourth argument will contain a list of content models build like this one. The"EMPTY","ANY", and"MIXED"types will only occur at top level. π
Examples, bestie:
proc elDeclHandler {name content} {
puts "$name $content"
}
set parser [expat -elementdeclcommand elDeclHandler]
$parser parse {<?xml version='1.0'?>
<!DOCTYPE test [
<!ELEMENT test (#PCDATA)>
]>
<test>foo</test>}
This would result in the following command being invoked:
test {MIXED {} {} {}}
$parser reset
$parser parse {<?xml version='1.0'?>
<!DOCTYPE test [
<!ELEMENT test (a|b)>
]>
<test><a/></test>}
This would result in the following command being invoked:
elDeclHandler test {CHOICE {} {} {{NAME {} a {}} {NAME {} b {}}}}
-attlistdeclcommandscript β names a Tcl command to associate with attlist declarations. The actual command is this option followed by five arguments. The Attlist declaration handler is called for each attribute. So a single Attlist declaration with multiple attributes declared will generate multiple calls to this handler. one declaration, many calls β it's giving overachiever. The arguments are the element name this attribute belongs to, the name of the attribute, the type of the attribute, the default value (may be the empty list) and a required flag. If this flag is true and the default value is not the empty list, then this is a"#FIXED"default.
Example:
proc attlistHandler {elname name type default isRequired} {
puts "$elname $name $type $default $isRequired"
}
set parser [expat -attlistdeclcommand attlistHandler]
$parser parse {<?xml version='1.0'?>
<!DOCTYPE test [
<!ELEMENT test EMPTY>
<!ATTLIST test
id ID #REQUIRED
name CDATA #IMPLIED>
]>
<test/>}
This would result in the following commands being invoked:
attlistHandler test id ID {} 1
attlistHandler test name CDATA {} 0
-startdoctypedeclcommandscript β names a Tcl command to associate with the start of the DOCTYPE declaration. This command is called before any DTD or internal subset is parsed. The actual command is this option followed by four arguments: the doctype name, the system identifier, the public identifier and a boolean, that shows if the DOCTYPE has an internal subset. π₯-enddoctypedeclcommandscript β names a Tcl command to associate with the end of the DOCTYPE declaration. This command is called after processing any external subset. It's called with no additional arguments. β-paramentityparsing never|notstandalone|alwaysβ"never"disables expansion of parameter entities,"always"expands always and"notstandalone"only if the document isn't"standalone='no'". The default ist"never". π§-entitydeclcommandscript β names a Tcl command to associate with any entity declaration. The actual command is this option followed by seven arguments: the entity name, a boolean identifying parameter entities, the value of the entity, the base uri, the system identifier, the public identifier and the notation name. According to the type of entity declaration some of this arguments may be the empty list. π-ignorewhitecdataboolean β if this flag is set, element content which contain only whitespaces isn't reported with the-characterdatacommand. π-ignorewhitespaceboolean β another name for-ignorewhitecdata; see there. π-handlersetname β this option sets the Tcl handler set scope for the configure options. Any option value pair following this option in the same call to the parser are modifying the named Tcl handler set. If you don't use this option, you are modifying the default Tcl handler set, named"default". π-noexpandboolean β normally, the parser will try to expand references to entities defined in the internal subset. If this option is set to a true value this entities are not expanded, but reported literal via the default handler.
β οΈ Warning, bestie, listen up: if you set this option to true and doesn't install a default handler (with the -defaultcommand option) for every handler set of the parser, all internal entities are silent lost for the handler sets without a default handler. Silent. Lost. No cap. π
-useForeignDTD<boolean>β if<boolean>is true and the document does not have an external subset, the parser will call the-externalentitycommandscript with empty values for thesystemIdandpublicIDarguments. This option must be set, before the first piece of data is parsed. Setting this option, after the parsing has started has no effect. The default is not to use a foreign DTD. The default is restored, after resetting the parser.
π Pleace notice, that a-paramentityparsingvalue of"never"(which is the default) suppresses any call to the-externalentitycommandscript.
π Pleace notice, that, if the document also doesn't have an internal subset, the-startdoctypedeclcommandandenddoctypedeclcommandscripts, if set, are not called.
-billionLaughsAttackProtectionMaximumAmplification<float> β bestie. okay. so. there's a whole Wikipedia article about this at <https://en.wikipedia.org/wiki/Billion_laughs_attack> and you should lowkey read it π. This option, together with-billionLaughsAttackProtectionActivationThreshold, is what gives you control over the parser limits that protect against billion laughs attacks. It wants a float that's>= 1.0, nothing less. And honestly? You should never need to touch this one, because the default value (100.0) should already work for any real data. If you ever find yourself needing to raise it for some non-attack payload, please report that β like, actually report it, that's a real thing they want to know π«Ά.-billionLaughsAttackProtectionActivationThreshold<long> β same Wikipedia article, same link, <https://en.wikipedia.org/wiki/Billion_laughs_attack>. This one is the other half of the billion-laughs-defense duo, working together with-billionLaughsAttackProtectionMaximumAmplificationto control the parser limits that protect against billion laughs attacks. It expects a positive integer as its argument. Again: you should never need to use this option, because the default (8388608) should work for any real data. And again: if you ever do need to bump it for non-attack payload, please report it. periodt β¨.
π COMMAND METHODS π
These are the methods you call on your parser command. Say hi to your parser, it's the main character now π.
parser configure option value ?option value?β sets configuration options for the parser. Every command option can be set or modified through this methodβ¦ except-namespace, that one does not play. One exception, that's it, that's the rule π .parser cget ?-handlerset name? optionβ returns the current configuration value of option for the parser. And if you pass in the-handlersetoption? Then you get the configuration back for the named handler set instead. Slay, per-set configs πΈ.parser currentmarkupβ hands you the current markup as found in the XML, but only if you call it from inside one of its markup event handler scripts (-elementstartcommand,-elementendcommand,-commentcommand, and-processinginstructioncommand). Call it from literally anywhere else and it returns the empty string. No markup for you, bestie, empty string only π«₯.parser deleteβ deletes the parser and the parser command. Gone. Poof. But a parser cannot be deleted from within one of its handler callbacks β neither directly nor indirectly β and if you try it anyway it will raise a Tcl error in this case. Don't do it. It's giving chaos.parser freeβ just another name to call the methoddelete, go look up there β¬οΈ. Same thing, different name, iconic behavior.parser getβ takes exactly one of these, and you can only request one value at a time:-specifiedattributecountβ returns the number of attribute/value pairs passed in the last call to theelementstartcommandthat were actually specified in the start-tag rather than defaulted in. Each attribute/value pair counts as2, so this corresponds to an index into the attribute list passed to theelementstartcommand. Math, but make it fashion π’.-idattributeindexβ returns the index of the ID attribute passed in the last call toXML_StartElementHandler, or-1if there is no ID attribute. Each attribute/value pair counts as2, so this corresponds to an index into the attributes list passed to theelementstartcommand.-currentbytecountβ returns the number of bytes in the current event. Returns0if the event is in an internal entity. Also, if you use this option, consider whether you may need the parser option-keepTextStartπ.-currentlinenumberβ returns the line number of the current parse location. And yep, if you use this option, consider whether you may need the parser option-keepTextStart.-currentcolumnnumberβ returns the column number of the current parse location. And again, if you use this option, consider whether you may need the parser option-keepTextStart.-currentbyteindexβ returns the byte index of the current parse location. And once more for the people in the back: if you use this option, consider whether you may need the parser option-keepTextStart.
parser parse dataβ parses the XML string data. The event callback scripts get called as their triggering events happen. This method cannot be used from within a callback (neither directly nor indirectly) of the parser that's being used, and it will raise an error in this case. Read that again. Not from a callback. Not even indirectly π .parser parsechannel channelIDβ reads the XML data out of the Tcl channel channelID (starting at the current access position, without any seek) up to the end of file condition, and parses that data. The channel encoding is respected, as it should be π. Use the helper proctDOM::xmlOpenFileout of the tDOM script library to open a file if you want to use this method. And this method cannot be used from within a callback (neither directly nor indirectly) of the parser to be used, and it will raise an error in this case.parser parsefile filenameβ reads the XML data directly out of the file named filename and parses that data. This is done with low-level file operations, very demure, very mindful π§. The XML data must be inUS-ASCII,ISO-8859-1,UTF-8, orUTF-16encoding. If applicable, this is the fastest way to parse XML data β speed queen π. And this method cannot be used from within a callback (neither directly nor indirectly) of the parser to be used, and it will raise an error in this case.parser resetβ resets the parser in preparation for parsing another document. Fresh start, clean slate, new era π. But a parser cannot be reset from within one of its handler callbacks (neither directly nor indirectly) and will raise a Tcl error in these cases.
π Callback Command Return Codes π
A script invoked for any of the parser callback commands β think -elementstartcommand, -elementendcommand, etc. β may return an error code other than "ok" or "error". And all callbacks may, in addition, return "break" or "continue". So many options, so much power β¨.
- If a callback script returns an
"error"error code, then processing of the document is terminated and the error is propagated in the usual fashion. It's over, the doc stops right there π. - If a callback script returns a
"break"error code, then all further processing of every handler script out of this Tcl handler set is suppressed for the further parsing. This does not influence any other handler set. Stay in your lane, handler set π. - If a callback script returns a
"continue"error code, then processing of the current element, and its children, ceases for every handler script out of this Tcl handler set, and processing continues with the next (sibling) element. And nope, this does not influence any other handler set either. Siblings only, bestie π¦. - If a callback script returns a
"return"error code, then parsing is canceled altogether, but no error is raised. Justβ¦ quietly leaving the function. No drama, no error, no cap π .
π SEE ALSO π
expatapi, tdom
β KEYWORDS β
SAX, push, pushparser
okay that's the whole page, you made it, I'm so proud of you π₯Ήπ This one's from the Tcl manual pages β source/version tag Tcl with the page marker expat(3tcl), so if you ever need to cite it, that's the drip. Go forth and parse responsibly, bestie. Mwah π«Άβ¨
π¦ where this page comes from
- debian trixie: tdom 0.9.5.1-3 Β· MPL-2.0
- ubuntu questing: tdom 0.9.5.1-3
- ubuntu resolute: tdom 0.9.6-2 Β· MPL-2.0
- ubuntu stonking: tdom 0.9.6-3 Β· MPL-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms