SWI-Prolog SGML/XML parser
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog SGML/XML parser
        • Introduction
        • Bluffer's Guide
        • Predicate Reference
          • Loading Structured Documents
          • Handling white-space
          • XML documents
          • DTD-Handling
          • Extracting a DTD
          • Parsing Primitives
          • Type checking
        • Stream encoding issues
        • library(xpath): Select nodes in an XML DOM
        • Processing Indexed Files
        • External entities
        • library(pwp): Prolog Well-formed Pages
        • Writing markup
        • Unsupported SGML features
        • Acknowledgements

3 Predicate Reference

3.1 Loading Structured Documents

SGML or XML files are loaded through the common predicate load_structure/3. This is a predicate with many options. For simplicity a number of commonly used shorthands are provided: load_sgml_file/2, load_xml_file/2, and load_html_file/2.

load_structure(+Source, -ListOfContent, +Options)
Parse Source and return the resulting structure in ListOfContent. Source is either a term of the format stream(StreamHandle) or a file-name. Options is a list of options controlling the conversion process.

A proper XML document contains only a single toplevel element whose name matches the document type. Nevertheless, a list is returned for consistency with the representation of element content. The ListOfContent consists of the following types:

Atom
Atoms are used to represent CDATA. Note this is possible in SWI-Prolog, as there is no length-limit on atoms and atom garbage collection is provided.
element(Name, ListAttributes, ListOfContent)
Name is the name of the element. Using SGML, which is case-insensitive, all element names are returned as lowercase atoms.

ListOfAttributes is a list of Name=Value pairs for attributes. Attributes of type CDATA are returned literal. Multi-valued attributes (NAMES, etc.) are returned as a list of atoms. Handling attributes of the types NUMBER and NUMBERS depends on the setting of the number(+NumberMode) attribute through set_sgml_parser/2 or load_structure/3. By default they are returned as atoms, but automatic conversion to Prolog integers is supported. ListOfContent defines the content for the element.

sdata(Text)
If an entity with declared content-type SDATA is encountered, this term is returned holding the data in Text.
ndata(Text)
If an entity with declared content-type NDATA is encountered, this term is returned holding the data in Text.
pi(Text)
If a processing instruction is encountered (<?...?>), Text holds the text of the processing instruction. Please note that the <?xml ...?> instruction is handled internally.

The Options list controls the conversion process. Currently defined options are below. Other options are passed to sgml_parse/2.

dtd(?DTD)
Reference to a DTD object. If specified, the <!DOCTYPE ...> declaration is ignored and the document is parsed and validated against the provided DTD. If provided as a variable, the created DTD is returned. See section 3.5.
dialect(+Dialect)
Specify the parsing dialect. Supported are sgml (default), html4, html5, html (same as html4, xhtml, xhtml5, xml and xmlns. See the option dialect of set_sgml_parser/2 for details.
shorttag(+Bool)
Define whether SHORTTAG abbreviation is accepted. The default is true for SGML mode and false for the XML modes. Without SHORTTAG, a / is accepted with warning as part of an unquoted attribute-value, though /> still closes the element-tag in XML mode. It may be set to false for parsing HTML documents to allow for unquoted URLs containing /.
space(+SpaceMode)
Sets the‘space-handling-mode' for the initial environment. This mode is inherited by the other environments, which can override the inherited value using the XML reserved attribute xml:space. See section 3.2.
number(+NumberMode)
Determines how attributes of type NUMBER and NUMBERS are handled. If token (default) they are passed as an atom. If integer the parser attempts to convert the value to an integer. If successful, the attribute is passed as a Prolog integer. Otherwise it is still passed as an atom. Note that SGML defines a numeric attribute to be a sequence of digits. The - sign is not allowed and 1 is different from 01. For this reason the default is to handle numeric attributes as tokens. If conversion to integer is enabled, negative values are silently accepted.
case_sensitive_attributes(+Boolean)
Treat attribute values as case sensitive. The default is true for XML and false for SGML and HTML dialects.
case_preserving_attributes(+Boolean)
Treat attribute values as case insensitive but do not alter their case. The default is false. Setting this option sets the case_sensitive_attributes to the same value. This option was added to support HTML quasi quotations and most likely has little value in other contexts.
system_entities(+Boolean)
Define whether SYSTEM entities are expanded. The default is false.
defaults(+Bool)
Determines how default and fixed values from the DTD are used. By default, defaults are included in the output if they do not appear in the source. If false, only the attributes occurring in the source are emitted.
entity(+Name, +Value)
Defines (overwrites) an entity definition. At the moment, only CDATA entities can be specified with this construct. Multiple entity options are allowed.
file(+Name)
Sets the name of the file on which errors are reported. Sets the linenumber to 1.
line(+Line)
Sets the starting line-number for reporting errors.
max_memory(+Max)
Sets the maximum buffer size in bytes available for input data and CDATA output. If this limit is reached a resource error is raised. Using max_memory(0) (the default) means no resource limit will be enforced.
cdata(+Representation)
Specify the representation of cdata elements. Supported are atom (default), and string. The choice is not obvious. Strings are allocated on the Prolog stacks and subject to normal stack garbage collection. They are quicker to create and avoid memory fragmentation. But, multiple copies of the same string are stored multiple times, while the text is shared if atoms are used. Strings are also useful for security sensitive information as they are invisible to other threads and cannot be enumerated using, e.g., current_atom/1. Finally, using strings allows for resource usage limits using the global stack limit (see set_prolog_stack/2).
attribute_value(+Representation)
Specify the representation of attribute values. Supported are atom (default), and string. See above for the advantages and disadvantages of using strings.
keep_prefix(+Boolean)
If true, xmlns namespaces with prefixes are returned as ns(Prefix, URI) terms. If false (default), the prefix is ignored and the xmlns namespace is returned as just the URI.

3.2 Handling white-space

SGML2PL has four modes for handling white-space. The initial mode can be switched using the space(SpaceMode) option to load_structure/3 and set_sgml_parser/2. In XML mode, the mode is further controlled by the xml:space attribute, which may be specified both in the DTD and in the document. The defined modes are:

space(sgml)
In SGML, newlines at the start and end of an element are removed.2In addition, newlines at the end of lines containing only markup should be deleted. This is not yet implemented. This is the default mode for the SGML dialect.
space(preserve)
White space is passed literally to the application. This mode leaves most white space handling to the application. This is the default mode for the XML dialect. Note that \r\n is still translated to \n. To preserve whitespace exactly, use space(strict) (see below)
space(strict)
White space is passed strictly to the application. This mode leaves all white space handling to the application. This is useful for producing and verifying XML signatures.
space(default)
In addition to sgml space-mode, all consequtive white-space is reduced to a single space-character. This mode canonicalises all white space.
space(remove)
In addition to default, all leading and trailing white-space is removed from CDATA objects. If, as a result, the CDATA becomes empty, nothing is passed to the application. This mode is especially handy for processing‘data-oriented' documents, such as RDF. It is not suitable for normal text documents. Consider the HTML fragment below. When processed in this mode, the spaces between the three modified words are lost. This mode is not part of any standard; XML 1.0 allows only default and preserve.
Consider adjacent <b>bold</b> <ul>and</ul> <it>italic</it> words.

3.3 XML documents

The parser can operate in two modes: sgml mode and xml mode, as defined by the dialect(Dialect) option. Regardless of this option, if the first line of the document reads as below, the parser is switched automatically into XML mode.

<?xml ... ?>

Currently switching to XML mode implies:

  • XML empty elements
    The construct <element [attribute...] /> is recognised as an empty element.

  • Predefined entities
    The following entitities are predefined: lt (<), gt (>), amp (&), apos (') and quot (").

  • Case sensitivity
    In XML mode, names are treated case-sensitive, except for the DTD reserved names (i.e. ELEMENT, etc.).

  • Character classes
    In XML mode, underscores (_) and colon (:) are allowed in names.

  • White-space handling
    White space mode is set to preserve. In addition to setting white-space handling at the toplevel the XML reserved attribute xml:space is honoured. It may appear both in the document and the DTD. The remove extension is honoured as xml:space value. For example, the DTD statement below ensures that the pre element preserves space, regardless of the default processing mode.
    <!ATTLIST pre xml:space nmtoken #fixed preserve>

3.3.1 XML Namespaces

Using the dialect xmlns, the parser will interpret XML namespaces. In this case, the names of elements are returned as a term of the format

URL:LocalName

If an identifier has no namespace and there is no default namespace it is returned as a simple atom. If an identifier has a namespace but this namespace is undeclared, the namespace name rather than the related URL is returned.

Attributes declaring namespaces (xmlns:<ns>=<url>) are reported as if xmlns were not a defined resource.

In many cases, getting attribute-names as url:name is not desirable. Such terms are hard to unify and sometimes multiple URLs may be mapped to the same identifier. This may happen due to poor version management, poor standardisation or because the the application doesn't care too much about versions. This package defines two call-backs that can be set using set_sgml_parser/2 to deal with this problem.

The call-back xmlns is called as XML namespaces are noticed. It can be used to extend a canonical mapping for later use by the urlns call-back. The following illustrates this behaviour. Any namespace containing rdf-syntax in its URL or that is used as rdf namespace is canonicalised to rdf. This implies that any attribute and element name from the RDF namespace appears as rdf:<name>

:- dynamic
        xmlns/3.

on_xmlns(rdf, URL, _Parser) :- !,
        asserta(xmlns(URL, rdf, _)).
on_xmlns(_, URL, _Parser) :-
        sub_atom(URL, _, _, _, 'rdf-syntax'), !,
        asserta(xmlns(URL, rdf, _)).

load_rdf_xml(File, Term) :-
        load_structure(File, Term,
                       [ dialect(xmlns),
                         call(xmlns, on_xmlns),
                         call(urlns, xmlns)
                       ]).

The library provides iri_xml_namespace/3 to break down an IRI into its namespace and localname:

[det]iri_xml_namespace(+IRI, -Namespace, -Localname)
Split an IRI (Unicode URI) into its Namespace (an IRI) and Localname (a Unicode XML name, see xml_name/2). The Localname is defined as the longest last part of the IRI that satisfies the syntax of an XML name. With IRI schemas that are designed to work with XML namespaces, this will typically break the IRI on the last # or /. Note however that this can produce unexpected results. E.g., in the example below, one might expect the namespace to be http://example.com/images\#, but an XML name cannot start with a digit.
?- iri_xml_namespace('http://example.com/images#12345', NS, L).
NS = 'http://example.com/images#12345',
L = ''.

As we see from the example above, the Localname can be the empty atom. Similarly, Namespace can be the empty atom if IRI is an XML name. Applications will often have to check for either or both these conditions. We decided against failing in these conditions because the application typically wants to know which of the two conditions (empty namespace or empty localname) holds. This predicate is often used for generating RDF/XML from an RDF graph.

[det]iri_xml_namespace(+IRI, -Namespace)
Same as iri_xml_namespace/3, but avoids creating an atom for the Localname.

3.4 DTD-Handling

The DTD (Document Type Definition) is a separate entity in sgml2pl, that can be created, freed, defined and inspected. Like the parser itself, it is filled by opening it as a Prolog output stream and sending data to it. This section summarises the predicates for handling the DTD.

new_dtd(+DocType, -DTD)
Creates an empty DTD for the named DocType. The returned DTD-reference is an opaque term that can be used in the other predicates of this package.
free_dtd(+DTD)
Deallocate all resources associated to the DTD. Further use of DTD is invalid.
load_dtd(+DTD, +File)
Define the DTD by loading the SGML-DTD file File. Same as load_dtd/3 with empty option list.
load_dtd(+DTD, +File, +Options)
Define the DTD by loading File. Defined options are the dialect option from open_dtd/3 and the encoding option from open/4. Notably the dialect option must match the dialect used for subsequent parsing using this DTD.
open_dtd(+DTD, +Options, -OutStream)
Open a DTD as an output stream. See load_dtd/2 for an example. Defined options are:
dialect(Dialect)
Define the DTD dialect. Default is sgml. Using xml or xmlns processes the DTD case-sensitive.
dtd(+DocType, -DTD)
Find the DTD representing the indicated doctype. This predicate uses a cache of DTD objects. If a doctype has no associated dtd, it searches for a file using the file search path dtd using the call:
...,
absolute_file_name(dtd(Type),
                   [ extensions([dtd]),
                     access(read)
                   ], DtdFile),
...

Note that DTD objects may be modified while processing errornous documents. For example, loading an SGML document starting with <?xml ...?> switches the DTD to XML mode and encountering unknown elements adds these elements to the DTD object. Re-using a DTD object to parse multiple documents should be restricted to situations where the documents processed are known to be error-free.

The DTD html is handled separately. The Prolog flag html_dialect specifies the default html dialect, which is either html4 or html5 (default).3Note that HTML5 has no DTD. The loaded DTD is an informal DTD that includes most of the HTML5 extensions (http://www.cs.tut.fi/~jkorpela/html5-dtd.html). In addition, the parser sets the dialect flag of the DTD object. This is used by the parser to accept HTML extensions. Next, the corresponding DTD is loaded.

dtd_property(+DTD, ?Property)
This predicate is used to examine the content of a DTD. Property is one of:
doctype(DocType)
An atom representing the document-type defined by this DTD.
elements(ListOfElements)
A list of atoms representing the names of the elements in this DTD.
element(Name, Omit, Content)
The DTD contains an element with the given name. Omit is a term of the format omit(OmitOpen, OmitClose), where both arguments are booleans (true or false representing whether the open- or close-tag may be omitted. Content is the content-model of the element represented as a Prolog term. This term takes the following form:
empty
The element has no content.
cdata
The element contains non-parsed character data. All data up to the matching end-tag is included in the data (declared content).
rcdata
As cdata, but entity-references are expanded.
any
The element may contain any number of any element from the DTD in any order.
#pcdata
The element contains parsed character data .
element(A)
n element with this name.
*(SubModel)
0 or more appearances.
?(SubModel)
0 or one appearance.
+(SubModel)
1 or more appearances.
,(SubModel1, SubModel2)
SubModel1 followed by SubModel2.
&(SubModel1, SubModel2)
SubModel1 and SubModel2 in any order.
|(SubModel1, SubModel2)
SubModel1 or SubModel2.
attributes(Element, ListOfAttributes)
ListOfAttributes is a list of atoms representing the attributes of the element Element.
attribute(Element, Attribute, Type, Default)
Query an element. Type is one of cdata, entity, id, idref, name, nmtoken, notation, number or nutoken. For DTD types that allow for a list, the notation list(Type) is used. Finally, the DTD construct (a|b|...) is mapped to the term nameof(ListOfValues).

Default describes the sgml default. It is one required, current, conref or implied. If a real default is present, it is one of default(Value) or fixed(Value).

entities(ListOfEntities)
ListOfEntities is a list of atoms representing the names of the defined entities.
entity(Name, Value)
Name is the name of an entity with given value. Value is one of
Atom
If the value is atomic, it represents the literal value of the entity.
system(Url)
Url is the URL of the system external entity.
public(Id, Url)
For external public entities, Id is the identifier. If an URL is provided this is returned in Url. Otherwise this argument is unbound.
notations(ListOfNotations)
Returns a list holding the names of all NOTATION declarations.
notation(Name, Decl)
Unify Decl with a list if system(+File) and/or public(+PublicId).

3.4.1 The DOCTYPE declaration

As this parser allows for processing partial documents and process the DTD separately, the DOCTYPE declaration plays a special role.

If a document has no DOCTYPE declaraction, the parser returns a list holding all elements and CDATA found. If the document has a DOCTYPE declaraction, the parser will open the element defined in the DOCTYPE as soon as the first real data is encountered.

3.5 Extracting a DTD

Some documents have no DTD. One of the neat facilities of this library is that it builds a DTD while parsing a document with an implicit DTD. The resulting DTD contains all elements encountered in the document. For each element the content model is a disjunction of elements and possibly #PCDATA that can be repeated. Thus, if we found element y and CDATA in element x, the model is:

<!ELEMENT x - - (y|#PCDATA)*>

Any encountered attribute is added to the attribute list with the type CDATA and default #IMPLIED.

The example below extracts the elements used in an unknown XML document.

elements_in_xml_document(File, Elements) :-
        load_structure(File, _,
                       [ dialect(xml),
                         dtd(DTD)
                       ]),
        dtd_property(DTD, elements(Elements)),
        free_dtd(DTD).

3.6 Parsing Primitives

new_sgml_parser(-Parser, +Options)
Creates a new parser. A parser can be used one or multiple times for parsing documents or parts thereof. It may be bound to a DTD or the DTD may be left implicit, in which case it is created from the document prologue or parsing is performed without a DTD. Options:
dtd(?DTD)
If specified with an initialised DTD, this DTD is used for parsing the document, regardless of the document prologue. If specified using as a variable, a reference to the created DTD is returned. This DTD may be created from the document prologue or build implicitely from the document's content.
free_sgml_parser(+Parser)
Destroy all resources related to the parser. This does not destroy the DTD if the parser was created using the dtd(DTD) option.
set_sgml_parser(+Parser, +Option)
Sets attributes to the parser. Currently defined attributes:
file(File)
Sets the file for reporting errors and warnings. Sets the line to 1.
line(Line)
Sets the current line. Useful if the stream is not at the start of the (file) object for generating proper line-numbers.
linepos(LinePos)
Sets notion of the current column in the source line.
charpos(Offset)
Sets the current character location. See also the file(File) option.
position(Position)
Set source location from a stream position term as obtained using stream_property(Stream, position(Position)).
dialect(Dialect)
Set the markup dialect. Known dialects:
sgml
The default dialect is to process as SGML. This implies markup is case-insensitive and standard SGML abbreviation is allowed (abreviated attributes and omitted tags).
html
html4
This is the same as sgml, but implies shorttag(false) and accepts XML empty element declarations (e.g., <img src="..."/>).
html5
In addition to html, accept attributes named data- without warning. This value initialises the charset to UTF-8.
xhtml
xhtml5
These document types are processed as xml. Dialect xhtml5 accepts attributes named data- without warning.
xml
This dialect is selected automatically if the processing instruction <?xml ...> is encountered. See section 3.3 for details.
xmlns
Process file as XML file with namespace support. See section 3.3.1 for details. See also the qualify_attributes option below.
xmlns(+URI)
Set the default namespace of the outer environment. This option is provided to process partial XML content with proper namespace resolution.
xmlns(+NS, +URI)
Specify a namespace for the outer environment. This option is provided to process partial XML content with proper namespace resolution.
qualify_attributes(Boolean)
How to handle unqualified attribute (i.e. without an explicit namespace) in XML namespace (xmlns) mode. Default and standard compliant is not to qualify such elements. If true, such attributes are qualified with the namespace of the element they appear in. This option is for backward compatibility as this is the behaviour of older versions. In addition, the namespace document suggests unqualified attributes are often interpreted in the namespace of their element.
space(SpaceMode)
Define the initial handling of white-space in PCDATA. This attribute is described in section 3.2.
number(NumberMode)
If token (default), attributes of type number are passed as a Prolog atom. If integer, such attributes are translated into Prolog integers. If the conversion fails (e.g. due to overflow) a warning is issued and the value is passed as an atom.
encoding(Encoding)
Set the initial encoding. The default initial encoding for XML documents is UTF-8 and for SGML documents ISO-8859-1. XML documents may change the encoding using the encoding= attribute in the header. Explicit use of this option is only required to parse non-conforming documents. Currently accepted values are iso-8859-1 and utf-8.
doctype(Element)
Defines the toplevel element expected. If a <!DOCTYPE declaration has been parsed, the default is the defined doctype. The parser can be instructed to accept the first element encountered as the toplevel using doctype(_). This feature is especially useful when parsing part of a document (see the parse option to sgml_parse/2.
get_sgml_parser(+Parser, -Option)
Retrieve infomation on the current status of the parser. Notably useful if the parser is used in the call-back mode. Currently defined options:
file(-File)
Current file-name. Note that this may be different from the provided file if an external entity is being loaded.
line(-Line)
Line-offset from where the parser started its processing in the file-object.
charpos(-CharPos)
Offset from where the parser started its processing in the file-object. See section 6.
charpos(-Start, -End)
Character offsets of the start and end of the source processed causing the current call-back. Used in PceEmacs to for colouring text in SGML and XML modes.
source(-Stream)
Prolog stream being processed. May be used in the on_begin, etc. callbacks from sgml_parse/2.
dialect(-Dialect)
Return the current dialect used by the parser (sgml, html, html5, xhtml, xhtml5, xml or xmlns).
event_class(-Class)
The event class can be requested in call-back events. It denotes the cause of the event, providing useful information for syntax highlighting. Defined values are:
explicit
The code generating this event is explicitely present in the document.
omitted
The current event is caused by the insertion of an omitted tag. This may be a normal event in SGML mode or an error in XML mode.
shorttag
The current event (begin or end) is caused by an element written down using the shorttag notation (<tag/value/>.
shortref
The current event is caused by the expansion of a shortref. This allows for highlighting shortref strings in the source-text.
doctype(-Element)
Return the defined document-type (= toplevel element). See also set_sgml_parser/2.
dtd(-DTD)
Return the currently used DTD. See dtd_property/2 for obtaining information on the DTD such as element and attribute properties.
context(-StackOfElements)
Returns the stack of currently open elements as a list. The head of this list is the current element. This can be used to determine the context of, for example, CDATA events in call-back mode. The elements are passed as atoms. Currently no access to the attributes is provided.
allowed(-Elements)
Determines which elements may be inserted at the current location. This information is returned as a list of element-names. If character data is allowed in the current location, #pcdata is part of Elements. If no element is open, the doctype is returned.

This option is intended to support syntax-sensitive editors. Such an editor should load the DTD, find an appropriate starting point and then feed all data between the starting point and the caret into the parser. Next it can use this option to determine the elements allowed at this point. Below is a code fragment illustrating this use given a parser with loaded DTD, an input stream and a start-location.

        ...,
        seek(In, Start, bof, _),
        set_sgml_parser(Parser, charpos(Start)),
        set_sgml_parser(Parser, doctype(_)),
        Len is Caret - Start,
        sgml_parse(Parser,
                   [ source(In),
                     content_length(Len),
                     parse(input)       % do not complete document
                   ]),
        get_sgml_parser(Parser, allowed(Allowed)),
        ...

sgml_parse(+Parser, +Options)
Parse an XML file. The parser can operate in two input and two output modes. Output is either a structured term as described with load_structure/2 or call-backs on predefined events. The first is especially suitable for manipulating not-too-large documents, while the latter provides a primitive means for handling very large documents.

Input is a stream. A full description of the option-list is below.

document(-Term)
A variable that will be unified with a list describing the content of the document (see load_structure/2).
source(+Stream)
An input stream that is read. This option must be given.
content_length(+Characters)
Stop parsing after Characters. This option is useful to parse input embedded in envelopes, such as the HTTP protocol.
cdata(+Representation)
Specify the representation of cdata elements. Supported are atom (default), and string. See load_structure/3 for details.
parse(+Unit)
Defines how much of the input is parsed. This option is used to parse only parts of a file.
file
Default. Parse everything upto the end of the input.
element
The parser stops after reading the first element. Using source(Stream), this implies reading is stopped as soon as the element is complete, and another call may be issued on the same stream to read the next element.
content
The value content is like element but assumes the element has already been opened. It may be used in a call-back from call(on_begin, Pred) to parse individual elements after validating their headers.
declaration
This may be used to stop the parser after reading the first declaration. This is especially useful to parse only the doctype declaration.
input
This option is intended to be used in conjunction with the allowed(Elements) option of get_sgml_parser/2. It disables the parser's default to complete the parse-tree by closing all open elements.
max_errors(+MaxErrors)
Set the maximum number of errors. If this number is exceeded further writes to the stream will yield an I/O error exception. Printing of errors is suppressed after reaching this value. The default is 50. Using max_errors(-1) makes the parser continue, no matter how many errors it encounters.
error(limit_exceeded(max_errors, Max), _)
syntax_errors(+ErrorMode)
Defines how syntax errors are handled.
quiet
Suppress all messages.
print
Default. Pass messages to print_message/2.
style
Print dubious input such as attempts for redefinitions in the DTD using print_message/2 with severity informational.
xml_no_ns(+Mode)
Error handling if an XML namespace is not defined. Default generates an error. If quiet, the error is suppressed. Can be used together with call(urlns, Closure) to provide external expansion of namespaces. See also section 3.3.1.
call(+Event, :PredicateName)
Issue call-backs on the specified events. PredicateName is the name of the predicate to call on this event, possibly prefixed with a module identifier. If the handler throws an exception, parsing is stopped and sgml_parse/2 re-throws the exception. The defined events are:
begin
An open-tag has been parsed. The named handler is called with three arguments: Handler(+Tag, +Attributes, +Parser).
end
A close-tag has been parsed. The named handler is called with two arguments: Handler(+Tag, +Parser).
cdata
CDATA has been parsed. The named handler is called with two arguments: Handler(+CDATA, +Parser), where CDATA is an atom representing the data.
pi
A processing instruction has been parsed. The named handler is called with two arguments: Handler(+Text, +Parser), where Text is the text of the processing instruction.
decl
A declaration (<!...>) has been read. The named handler is called with two arguments: Handler(+Text, +Parser), where Text is the text of the declaration with comments removed.

This option is expecially useful for highlighting declarations and comments in editor support, where the location of the declaration is extracted using get_sgml_parser/2.

error
An error has been encountered. the named handler is called with three arguments: Handler(+Severity, +Message, +Parser), where Severity is one of warning or error and Message is an atom representing the diagnostic message. The location of the error can be determined using get_sgml_parser/2

If this option is present, errors and warnings are not reported using print_message/3

xmlns
When parsing an in xmlns mode, a new namespace declaraction is pushed on the environment. The named handler is called with three arguments: Handler(+NameSpace, +URL, +Parser). See section 3.3.1 for details.
urlns
When parsing an in xmlns mode, this predicate can be used to map a url into either a canonical URL for this namespace or another internal identifier. See section 3.3.1 for details.

3.6.1 Partial Parsing

In some cases, part of a document needs to be parsed. One option is to use load_structure/2 or one of its variations and extract the desired elements from the returned structure. This is a clean solution, especially on small and medium-sized documents. It however is unsuitable for parsing really big documents. Such documents can only be handled with the call-back output interface realised by the call(Event, Action) option of sgml_parse/2. Event-driven processing is not very natural in Prolog.

The SGML2PL library allows for a mixed approach. Consider the case where we want to process all descriptions from RDF elements in a document. The code below calls process_rdf_description(Element) on each element that is directly inside an RDF element.

:- dynamic
        in_rdf/0.

load_rdf(File) :-
        retractall(in_rdf),
        open(File, read, In),
        new_sgml_parser(Parser, []),
        set_sgml_parser(Parser, file(File)),
        set_sgml_parser(Parser, dialect(xml)),
        sgml_parse(Parser,
                   [ source(In),
                     call(begin, on_begin),
                     call(end, on_end)
                   ]),
        close(In).

on_end('RDF', _) :-
        retractall(in_rdf).

on_begin('RDF', _, _) :-
        assert(in_rdf).
on_begin(Tag, Attr, Parser) :-
        in_rdf, !,
        sgml_parse(Parser,
                   [ document(Content),
                     parse(content)
                   ]),
        process_rdf_description(element(Tag, Attr, Content)).

3.7 Type checking

xml_is_dom(@Term)
True if Term is an SGML/XML term as produced by one of the above predicates and acceptable by xml_write/3 and friends.