TclXML

Contents

TclXML ? XML parser support for Tcl

Synopsis

package require xml
package require parserclass

package require xml ?3.3?
::xml::parserclass option ?arg arg...?
::xml::parser ?name? ? -optionvalue...?
parser option arg

Tcl Namespace Usage


::xml
::sgml
::xml::tclparser
::xml::libxml2

Description

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 xml::libxml2. In all cases, all available parser classes are registered with the TclXML package, the difference is simply in which one becomes the default.

Commands

::xml::parserclass

The ::xml::parserclass command is used to manage XML parser classes.

Command Options

The following command options may be used:

create
create name ? -createcommandscript? ? -createentityparsercommandscript? ? -parsecommandscript? ? -configurecommandscript? ? -getcommandscript? ? -deletecommandscript?

Creates an XML parser class with the given name.

destroy
destroy name

Destroys an XML parser class.

info
::xml::parserclass info names default

Returns information about registered XML parser classes.

::xml::parser

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.

Configuration Options

The ::xml::parser command accepts the following configuration options:

-attlistdeclcommand

-attlistdeclcommand script

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:

name Element type name
attrname Attribute name being declared
type Attribute type
default Attribute default, such as #IMPLIED
value Default attribute value. Empty string if none given.
-baseuri -baseurl

-baseuri URI

-baseurl URI

Specifies the base URI for resolving relative URIs that may be used in the XML document to refer to external entities.

-baseurl is deprecated in favour of -baseuri.

-characterdatacommand

-characterdatacommand script

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:

data Character data in the document
-commentcommand

-commentcommand script

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:

data Comment data
-defaultcommand

-defaultcommand script

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:

data Document data
-defaultexpandinternalentities

-defaultexpandinternalentities boolean

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.

-doctypecommand

-doctypecommand script

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:

name The name of the document element
public Public identifier for the external DTD subset
system System identifier for the external DTD subset. Usually a URI.
dtd The internal DTD subset

See also -startdoctypedeclcommand and -enddoctypedeclcommand.

-elementdeclcommand

-elementdeclcommand script

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:

name The element type name
model Content model specification
-elementendcommand

-elementendcommand script

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:

name The element type name that has ended
args Additional information about this element

Additional information about the element takes the form of configuration options. Possible options are:

-empty boolean The empty element syntax was used for this element
-namespace uri The element is in the XML namespace associated with the given URI
-elementstartcommand

-elementstartcommand script

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:

name The element type name that has started
attlist A Tcl list containing the attributes for this element. The list of attributes is formatted as pairs of attribute names and their values.
args Additional information about this element

Additional information about the element takes the form of configuration options. Possible options are:

-empty boolean The empty element syntax was used for this element
-namespace uri The element is in the XML namespace associated with the given URI
-namespacedecls list The start tag included one or more XML Namespace declarations. list is a Tcl list giving the namespaces declared. The list is formatted as pairs of values, the first value is the namespace URI and the second value is the prefix used for the namespace in this document. A default XML namespace declaration will have an empty string for the prefix.
-encoding

-encoding value

Gives the character encoding of the document. This option only has an effect before a document is parsed. The default character encoding is utf-8. If the value unknown is given, or any value other than utf-8, then the document text is treated as binary data. If the value is given as unknown then the parser will attempt to automatically determine the character encoding of the document (using byte-order-marks, etc). If any value other than utf-8 or unknown is given then the parser will read the document text using that character encoding.

-endcdatasectioncommand

-endcdatasectioncommand script

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.

-enddoctypedeclcommand

-enddoctypedeclcommand script

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.

-entitydeclcommand

-entitydeclcommand script

Specifies the prefix of a Tcl command to be evaluated when an entity declaration is encountered. The command evaluated is:

script name args

where:

name The name of the entity being declared
args Additional information about the entity declaration. An internal entity shall have a single argument, the replacement text. An external parsed entity shall have two additional arguments, the public and system indentifiers of the external resource. An external unparsed entity shall have three additional arguments, the public and system identifiers followed by the notation name.
-entityreferencecommand

-entityreferencecommand script

Specifies the prefix of a Tcl command to be evaluated when an entity reference is encountered. The command evaluated is:

script name

where:

name The name of the entity being referenced
-errorcommand

-errorcommand script

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:

errorcode A single word description of the error, intended for use by an application
errormsg A human-readable description of the error
-externalentitycommand

-externalentitycommand script

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:

name The Tcl command name of the current parser
baseuri An absolute URI for the current entity which is to be used to resolve relative URIs
uri The system identifier of the external entity, usually a URI
id The public identifier of the external entity. If no public identifier was given in the entity declaration then id will be an empty string.

The return result of the callback script determines the action of the parser. Note that these codes are interpreted in a different manner to other callbacks.

TCL_OK

The return result of the callback script is used as the external entity's data. The URI passed to the callback script is used as the entity's base URI.

This is useful to either override the normal loading of an entity's data, or to implement new or alternative URI schemes. As an example, the script below sets an external entity handler that intercepts "tcl:" URIs and evaluates them as inline Tcl scripts:

package require xml

proc External {name baseuri uri id} {
    switch -glob -,ecommandscript? ? -createentityparsercommandscript? ? -parsecommandscript? ? -configurecommandscript? ? -getcommandscript? ? -deletecommandscript?

Creates an XML parser class with the given name.

destroy
destroy name

Destroys an XML parser class.

info
::xml::parserclass info names default

Returns information about registered XML parser classes.

::xml::parser

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.

Configuration Options

The ::xml::parser command accepts the following configuration options:

-attlistdeclcommand

-attlistdeclcommand script

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:

name Element type name
attrname Attribute name being declared
type Attribute type
default Attribute default, such as #IMPLIED
value Default attribute value. Empty string if none given.
-baseuri -baseurl

-baseuri URI

-baseurl URI

Specifies the base URI for resolving relative URIs that may be used in the XML document to refer to external entities.

-baseurl is deprecated in favour of -baseuri.

-characterdatacommand

-characterdatacommand script

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:

data Character data in the document
-commentcommand

-commentcommand script

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:

data Comment data
-defaultcommand

-defaultcommand script

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:

data Document data
-defaultexpandinternalentities

-defaultexpandinternalentities boolean

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.

-doctypecommand

-doctypecommand script

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:

name The name of the document element
public Public identifier for the external DTD subset
system System identifier for the external DTD subset. Usually a URI.
dtd The internal DTD subset

See also -startdoctypedeclcommand and -enddoctypedeclcommand.

-elementdeclcommand

-elementdeclcommand script

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:

name The element type name
model Content model specification
-elementendcommand

-elementendcommand script

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:

name The element type name that has ended
args Additional information about this element

Additional information about the element takes the form of configuration options. Possible options are:

-empty boolean The empty element syntax was used for this element
-namespace uri The element is in the XML namespace associated with the given URI
-elementstartcommand

-elementstartcommand script

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:

name The element type name that has started
attlist A Tcl list containing the attributes for this element. The list of attributes is formatted as pairs of attribute names and their values.
args Additional information about this element

Additional information about the element takes the form of configuration options. Possible options are:

-empty boolean The empty element syntax was used for this element
-namespace uri The element is in the XML namespace associated with the given URI
-namespacedecls list The start tag included one or more XML Namespace declarations. list is a Tcl list giving the namespaces declared. The list is formatted as pairs of values, the first value is the namespace URI and the second value is the prefix used for the namespace in this document. A default XML namespace declaration will have an empty string for the prefix.
-encoding

-encoding value

Gives the character encoding of the document. This option only has an effect before a document is parsed. The default character encoding is utf-8. If the value unknown is given, or any value other than utf-8, then the document text is treated as binary data. If the value is given as unknown then the parser will attempt to automatically determine the character encoding of the document (using byte-order-marks, etc). If any value other than utf-8 or unknown is given then the parser will read the document text using that character encoding.

-endcdatasectioncommand

-endcdatasectioncommand script

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.

-enddoctypedeclcommand

-enddoctypedeclcommand script

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.

-entitydeclcommand

-entitydeclcommand script

Specifies the prefix of a Tcl command to be evaluated when an entity declaration is encountered. The command evaluated is:

script name args

where:

name The name of the entity being declared
args Additional information about the entity declaration. An internal entity shall have a single argument, the replacement text. An external parsed entity shall have two additional arguments, the public and system indentifiers of the external resource. An external unparsed entity shall have three additional arguments, the public and system identifiers followed by the notation name.
-entityreferencecommand

-entityreferencecommand script

Specifies the prefix of a Tcl command to be evaluated when an entity reference is encountered. The command evaluated is:

script name

where:

name The name of the entity being referenced
-errorcommand

-errorcommand script

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:

errorcode A single word description of the error, intended for use by an application
errormsg A human-readable description of the error
-externalentitycommand

-externalentitycommand script

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:

name The Tcl command name of the current parser
baseuri An absolute URI for the current entity which is to be used to resolve relative URIs
uri The system identifier of the external entity, usually a URI
id The public identifier of the external entity. If no public identifier was given in the entity declaration then id will be an empty string.

The return result of the callback script determines the action of the parser. Note that these codes are interpreted in a different manner to other callbacks.

TCL_OK

The return result of the callback script is used as the external entity's data. The URI passed to the callback script is used as the entity's base URI.

This is useful to either override the normal loading of an entity's data, or to implement new or alternative URI schemes. As an example, the script below sets an external entity handler that intercepts "tcl:" URIs and evaluates them as inline Tcl scripts:

package require xml

proc External {name baseuri uri id} {
    switch -glob -,ecommandscript? ? -createentityparsercommandscript? ? -parsecommandscript? ? -configurecommandscript? ? -getcommandscript? ? -deletecommandscript?

Creates an XML parser class with the given name.

destroy
destroy name

Destroys an XML parser class.

info
::xml::parserclass info names default

Returns information about registered XML parser classes.

::xml::parser

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.

Configuration Options

The ::xml::parser command accepts the following configuration options:

-attlistdeclcommand

-attlistdeclcommand script

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:

name Element type name
attrname Attribute name being declared
type Attribute type
default Attribute default, such as #IMPLIED
value Default attribute value. Empty string if none given.
-baseuri -baseurl

-baseuri URI

-baseurl URI

Specifies the base URI for resolving relative URIs that may be used in the XML document to refer to external entities.

-baseurl is deprecated in favour of -baseuri.

-characterdatacommand

-characterdatacommand script

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:

data Character data in the document
-commentcommand

-commentcommand script

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:

data Comment data
-defaultcommand

-defaultcommand script

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:

data Document data
-defaultexpandinternalentities

-defaultexpandinternalentities boolean

Specifies whether entities declared in the internal DTD subset are expanded wi