TclXML(n) | TclXML(n) |
::xml::parser - XML parser support for Tcl
package require xml
package require parserclass
xml2.6
::xml::sgml::xml::tclparser
::xml::parserclass option ? arg arg ... ?
::xml::parser ? name? ? -option value ... ?
parser option arg
TclXML provides event-based parsing of XML documents. The application may register callback scripts for certain document features, and when the parser encounters those features while parsing the document the callback is evaluated.
The parser may also perform other functions, such as normalisation, validation and/or entity expansion. Generally, these functions are under the control of configuration options. Whether these functions can be performed at all depends on the parser implementation.
The TclXML package provides a generic interface for use by a Tcl application, along with a low-level interface for use by a parser implementation. Each implementation provides a class of XML parser, and these register themselves using the ::xml::parserclass create command. One of the registered parser classes will be the default parser class.
Loading the package with the generic package require xml command allows the package to automatically determine the default parser class. In order to select a particular parser class as the default, that class' package may be loaded directly, eg. package require expat. In all cases, all available parser classes are registered with the TclXML package, the difference is simply in which one becomes the default.
The ::xml::parserclass command is used to manage XML parser classes.
The following command options may be used:
Creates an XML parser class with the given name.
Destroys an XML parser class.
Returns information about registered XML parser classes.
The ::xml::parser command creates an XML parser object. The return value of the command is the name of the newly created parser.
The parser scans an XML document's syntactical structure, evaluating callback scripts for each feature found. At the very least the parser will normalise the document and check the document for well-formedness. If the document is not well-formed then the -errorcommand option will be evaluated. Some parser classes may perform additional functions, such as validation. Additional features provided by the various parser classes are described in the section Parser Classes
Parsing is performed synchronously. The command blocks until the entire document has been parsed. Parsing may be terminated by an application callback, see the section Callback Return Codes. Incremental parsing is also supported by using the -final configuration option.
The ::xml::parser command accepts the following configuration options:
Specifies the prefix of a Tcl command to be evaluated whenever an attribute list declaration is encountered in the DTD subset of an XML document. The command evaluated is: script name attrname type default value
where:
Specifies the base URI for resolving relative URIs that may be used in the XML document to refer to external entities.
Specifies the prefix of a Tcl command to be evaluated whenever character data is encountered in the XML document being parsed. The command evaluated is: script data
where:
Specifies the prefix of a Tcl command to be evaluated whenever a comment is encountered in the XML document being parsed. The command evaluated is: script data
where:
Specifies the prefix of a Tcl command to be evaluated when no other callback has been defined for a document feature which has been encountered. The command evaluated is: script data
where:
Specifies whether entities declared in the internal DTD subset are expanded with their replacement text. If entities are not expanded then the entity references will be reported with no expansion.
Specifies the prefix of a Tcl command to be evaluated when the document type declaration is encountered. The command evaluated is: script name public system dtd
where:
See also -startdoctypedeclcommand and -enddoctypedeclcommand.
Specifies the prefix of a Tcl command to be evaluated when an element markup declaration is encountered. The command evaluated is: script name model
where:
Specifies the prefix of a Tcl command to be evaluated when an element end tag is encountered. The command evaluated is: script name args
where:
Additional information about the element takes the form of configuration options. Possible options are:
Specifies the prefix of a Tcl command to be evaluated when an element start tag is encountered. The command evaluated is: script name attlist args
where:
Additional information about the element takes the form of configuration options. Possible options are:
Specifies the prefix of a Tcl command to be evaluated when end of a CDATA section is encountered. The command is evaluated with no further arguments.
Specifies the prefix of a Tcl command to be evaluated when end of the document type declaration is encountered. The command is evaluated with no further arguments.
Specifies the prefix of a Tcl command to be evaluated when an entity declaration is encountered. The command evaluated is: script name args
where:
Specifies the prefix of a Tcl command to be evaluated when an entity reference is encountered. The command evaluated is: script name
where:
Specifies the prefix of a Tcl command to be evaluated when a fatal error is detected. The error may be due to the XML document not being well-formed. In the case of a validating parser class, the error may also be due to the XML document not obeying validity constraints. By default, a callback script is provided which causes an error return code, but an application may supply a script which attempts to continue parsing. The command evaluated is: script errorcode errormsg
where:
Specifies the prefix of a Tcl command to be evaluated to resolve an external entity reference. If the parser has been configured to validate the XML document, a default script is supplied that resolves the URI given as the system identifier of the external entity and recursively parses the entity's data. If the parser has been configured as a non-validating parser, then by default external entities are not resolved. This option can be used to override the default behaviour. The command evaluated is: script name baseuri uri id
where:
Specifies whether the XML document being parsed is complete. If the document is to be incrementally parsed then this option will be set to false, and when the last fragment of document is parsed it is set to true. For example,
set parser [::xml::parser -final 0] $parser parse $data1 $parser parse $data2 $parser configure -final 1 $parser parse $finaldata
If this option is set to true then spans of character data in the XML document which are composed only of white-space (CR, LF, space, tab) will not be reported to the application. In other words, the data passed to every invocation of the -characterdatacommand script will contain at least one non-white-space character.
Specifies the prefix of a Tcl command to be evaluated when a notation declaration is encountered. The command evaluated is: script name uri
where:
Specifies the prefix of a Tcl command to be evaluated when the parser determines that the XML document being parsed is not a standalone document.
Controls whether external parameter entities are parsed.
Specifies the prefix of a Tcl command to be evaluated when a parameter entity declaration is encountered. The command evaluated is: script name args
where:
The name of the parser class to instantiate for this parser object. This option may only be specified when the parser instance is created.
Specifies the prefix of a Tcl command to be evaluated when a processing instruction is encountered. The command evaluated is: script target data
where:
If this option is enabled then when an element is encountered that uses the special empty element syntax, additional arguments are appended to the -elementstartcommand and -elementendcommand callbacks. The arguments -empty 1 are appended. For example: script -empty 1
Specifies the prefix of a Tcl command to be evaluated when the start of a CDATA section section is encountered. No arguments are appended to the script.
Specifies the prefix of a Tcl command to be evaluated at the start of a document type declaration. No arguments are appended to the script.
Specifies the prefix of a Tcl command to be evaluated when a character is encountered with an unknown encoding. This option has not been implemented.
Specifies the prefix of a Tcl command to be evaluated when a declaration is encountered for an unparsed entity. The command evaluated is: script system public notation
where:
Enables validation of the XML document to be parsed. Any changes to this option are ignored after an XML document has started to be parsed, but the option may be changed after a reset.
Specifies the prefix of a Tcl command to be evaluated when a warning condition is detected. A warning condition is where the XML document has not been authored correctly, but is still well-formed and may be valid. For example, the special empty element syntax may be used for an element which has not been declared to have empty content. By default, a callback script is provided which silently ignores the warning. The command evaluated is: script warningcode warningmsg
where:
Specifies the prefix of a Tcl command to be evaluated when the XML declaration is encountered. The command evaluated is: script version encoding standalone
where:
The ::xml::parser command creates a new Tcl command with the same name as the parser. This command may be used to invoke various operations on the parser object. It has the following general form: name option arg
option and the arg determine the exact behaviour of the command. The following commands are possible for parser objects:
Returns the current value of the configuration option given by option. Option may have any of the values accepted by the parser object.
Modify the configuration options of the parser object. Option may have any of the values accepted by the parser object.
Creates a new parser object. The new object inherits the same configuration options as the parent parser object, but is able to parse XML data in a parsed entity. The option -dtdsubset allows markup declarations to be treated as being in the internal or external DTD subset.
Frees all resources associated with the parser object. The object is not usable after this command has been invoked.
Returns information about the XML document being parsed. Each parser class provides different information, see the documentation for the parser class.
Parses the XML document. The usual desired effect is for various application callbacks to be evaluated. Other functions will also be performed by the parser class, at the very least this includes checking the XML document for well-formedness.
Initialises the parser object in preparation for parsing a new XML document.
Every callback script evaluated by a parser may return a return code other than TCL_OK. Return codes are interpreted as follows:
This script outputs the character data of an XML document read from stdin.
package require xml proc cdata {data args} { puts -nonewline $data } set parser [::xml::parser -characterdatacommand cdata] $parser parse [read stdin]
This script counts the number of elements in an XML document read from stdin.
package require xml proc EStart {varName name attlist args} { upvar #0 $varName var incr var } set count 0 set parser [::xml::parser -elementstartcommand [list EStart count]] $parser parse [read stdin] puts "The XML document contains $count elements"
This section will discuss how a parser class is implemented.
The pure-Tcl parser class requires no compilation - it is a collection of Tcl scripts. This parser implementation is non-validating, ie. it can only check well-formedness in a document. However, by enabling the -validate option it will read the document's DTD and resolve external entities.
This parser implementation aims to implement XML v1.0 and supports XML Namespaces.
Generally the parser produces XML Infoset information items. That is, it gives the application a slightly higher-level view than the raw XML syntax. For example, it does not report CDATA Sections.
This section will discuss the Expat parser class.
TclDOM, a Tcl interface for the W3C Document Object Model.
Tcl | Tcl Built-In Commands |