DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

XML::Checker



NAME

XML::Checker - A perl module for validating XML


SYNOPSIS

the XML::Checker::Parser manpage - an the XML::Parser manpage that validates at parse time

the XML::DOM::ValParser manpage - an the XML::DOM::Parser manpage that validates at parse time

(Some of the package names may change! This is only an alpha release...)


DESCRIPTION

XML::Checker can be used in different ways to validate XML. See the manual pages of the XML::Checker::Parser manpage and the XML::DOM::ValParser manpage for more information.

This document only describes common topics like error handling and the XML::Checker class itself.

WARNING: Not all errors are currently checked. Almost everything is subject to change. Some reported errors may not be real errors.


ERROR HANDLING

Whenever XML::Checker (or one of the packages that uses XML::Checker) detects a potential error, the 'fail handler' is called. It is currently also called to report information, like how many times an Entity was referenced. (The whole error handling mechanism is subject to change, I'm afraid...)

The default fail handler is XML::Checker::print_error(), which prints an error message to STDERR. It does not stop the XML::Checker, so it will continue looking for other errors. The error message is created with XML::Checker::error_string().

You can define your own fail handler in two ways, locally and globally. Use a local variable to temporarily override the fail handler. This way the default fail handler is restored when the local variable goes out of scope, esp. when exceptions are thrown e.g.

 # Using a local variable to temporarily override the fail handler (preferred)
 { # new block - start of local scope
   local $XML::Checker::FAIL = \&my_fail;
   ... your code here ...
 } # end of block - the previous fail handler is restored

You can also set the error handler globally, risking that your code may not be reusable or may clash with other modules that use XML::Checker.

 # Globally setting the fail handler (not recommended)
 $XML::Checker::FAIL = \&my_fail;
 ... rest of your code ...

The fail handler is called with the following parameters ($code, $msg, @context), where $code is the error code, $msg is the error description and @context contains information on where the error occurred. The @context is a (ordered) list of (key,value) pairs and can easily be turned into a hash. It contains the following information:

 Element - tag name of Element node (if applicable)
 Attr - attribute name (if applicable)
 ChildElementIndex - if applicable (see error 157)
 line - only when parsing
 column - only when parsing
 byte - only when parsing (-1 means: end of file)

Some examples of fail handlers:

 # Don't print info messages
 sub my_fail
 {
     my $code = shift;
     print STDERR XML::Checker::error_message ($code, @_)
         if $code < 300;
 }
 # Die when the first error is encountered - this will stop
 # the parsing process. Ignore information messages.
 sub my_fail
 {
     my $code = shift;
     die XML::Checker::error_message ($code, @_) if $code < 300;
 }
 # Count the number of undefined NOTATION references
 # and print the error as usual
 sub my_fail
 {
     my $code = shift;
     $count_undef_notations++ if $code == 100;
     XML::Checker::print_error ($code, @_);
 }
 # Die when an error is encountered.
 # Don't die if a warning or info message is encountered, just print a message.
 sub my_fail {
     my $code = shift;
     die XML::Checker::error_string ($code, @_) if $code < 200;
     XML::Checker::print_error ($code, @_);
 }


INSIGNIFICANT WHITESPACE

XML::Checker keeps track of whether whitespace found in character data is significant or not. It is considered insignicant if it is found inside an element that has a ELEMENT rule that is not of type Mixed or of type ANY. (A Mixed ELEMENT rule does contains the #PCDATA keyword. An ANY rule contains the ANY keyword. See the XML spec for more info.)

XML::Checker can not determine whether whitespace is insignificant in those two cases, because they both allow regular character data to appear within XML elements and XML::Checker can therefore not deduce whether whitespace is part of the actual data or was just added for readability of the XML file.

XML::Checker::Parser and XML::DOM::ValParser both have the option to skip insignificant whitespace when setting SkipInsignifWS to 1 in their constructor. If set, they will not call the Char handler when insignificant whitespace is encountered. This means that in XML::DOM::ValParser no Text nodes are created for insignificant whitespace.

Regardless of whether the SkipInsignifWS options is set, XML::Checker always keeps track of whether whitespace is insignificant. After making a call to XML::Checker's Char handler, you can find out if it was insignificant whitespace by calling the isInsignifWS method.

When using multiple (nested) XML::Checker instances or when using XML::Checker without using XML::Checker::Parser or XML::DOM::ValParser (which hardly anybody probably will), make sure to set a local variable in the scope of your checking code, e.g.

  { # new block - start of local scope
    local $XML::Checker::INSIGNIF_WS = 0;
    ... insert your code here ...
  } # end of scope


ERROR CODES

There are 3 categories, errors, warnings and info messages. (The codes are still subject to change, as well the error descriptions.)

Most errors have a link to the appropriate Validaty Constraint (VC) or other section in the XML specification.

ERROR Messages

100 - 109

110 - 119

120 - 129

130 - 139

150 - 159

160 - 169

WARNING Messages (200 and up)

INFO Messages (300 and up)

Not checked

The following errors are already checked by the XML::Parser manpage (expat) and are currently not checked by XML::Checker:

(?? TODO - add more info)

root element is missing

the XML::Parser manpage (expat) throws 'no element found' exception. See no_root.xml for an example.


XML::Checker

XML::Checker can be easily plugged into your application. It uses mostly the same style of event handlers (or callbacks) as the XML::Parser manpage. See the XML::Parser manpage manual page for descriptions of most handlers.


It also implements PerlSAX style event handlers. See L<PerlSAX interface>.

Currently, the XML::Checker object is a blessed hash with the following (potentially useful) entries:

 $checker->{RootElement} - root element name as found in the DOCTYPE
 $checker->{NOTATION}->{$notation} - is 1 if the NOTATION was defined
 $checker->{ENTITY}->{$name} - contains the (first) ENTITY value if defined
 $checker->{Unparsed}->{$entity} - is 1 if the unparsed ENTITY was defined
 $checker->{ID}->{$id} - is 1 if the ID was defined
 $checker->{IDREF}->{$id} - number of times the ID was referenced
 # Less useful:
 $checker->{ERule}->{$tag} - the ELEMENT rules by Element tag name
 $checker->{ARule}->{$tag} - the ATTLIST rules by Element tag name
 $checker->{Context} - context stack used internally
 $checker->{CurrARule} - current ATTLIST rule for the current Element

XML:Checker methods

This section is only interesting when using XML::Checker directly. XML::Checker supports most event handlers that the XML::Parser manpage supports with minor differences. Note that the XML::Checker event handler methods are instance methods and not static, so don't forget to call them like this, without passing $expat (as in the the XML::Parser manpage) handlers:

 $checker->Start($tagName);
Constructor
 $checker = new XML::Checker;
 $checker = new XML::Checker (%user_args);

User data may be stored by client applications. Only $checker->{User} is guaranteed not to clash with internal hash keys.

getRootElement ()
 $tagName = $checker->getRootElement;

Returns the root element name as found in the DOCTYPE

Expat interface

XML::Checker supports what I call the Expat interface, which is the collection of methods you normally specify as the callback handlers when using XML::Parser.

Only the following the XML::Parser manpage handlers are currently supported: Init, Final, Char, Start, End, Element, Attlist, Doctype, Unparsed, Entity, Notation.

I don't know how to correctly support the Default handler for all the XML::Parser manpage releases. The Start handler works a little different (see below) and I added Attr, InitDomElem, FinalDomElem, CDATA and EntityRef handlers. See the XML::Parser manpage for a description of the handlers that are not listed below.

Note that this interface may disappear, when the PerlSAX interface stabilizes.

Start ($tag)
 $checker->Start($tag);

Call this when an Element with the specified $tag name is encountered. Different from the Start handler in the XML::Parser manpage, in that no attributes are passed in (use the Attr handler for those.)

Attr ($tag, $attrName, $attrValue, $isSpecified)
 $checker->Attr($tag,$attrName,$attrValue,$spec);

Checks an attribute with the specified $attrName and $attrValue against the ATTLIST definition of the element with the specified $tag name. $isSpecified means whether the attribute was specified (1) or defaulted (0).

EndAttr ()
 $checker->EndAttr;

This should be called after all attributes are passed with Attr(). It will check which of the #REQUIRED attributes were not specified and generate the appropriate error (159) for each one that is missing.

CDATA ($text)
 $checker->CDATA($text);

This should be called whenever CDATASections are encountered. Similar to Char handler (but might perform different checks later...)

EntityRef ($entity, $isParameterEntity)
 $checker->EntityRef($entity,$isParameterEntity);

Checks the ENTITY reference. Set $isParameterEntity to 1 for entity references that start with '%'.

InitDomElem () and FinalDomElem ()

Used by XML::DOM::Element::check() to initialize (and cleanup) the context stack when checking a single element.

PerlSAX interface

XML::Checker now also supports the PerlSAX interface, so you can use XML::Checker wherever you use PerlSAX handlers.

XML::Checker implements the following methods: start_document, end_document, start_element, end_element, characters, processing_instruction, comment, start_cdata, end_cdata, entity_reference, notation_decl, unparsed_entity_decl, entity_decl, element_decl, attlist_decl, doctype_decl, xml_decl

Not implemented: set_document_locator, ignorable_whitespace

See PerlSAX.pod for details. (It is called lib/PerlSAX.pod in the libxml-perl distribution which can be found at CPAN.)


CAVEATS

This is an alpha release. Almost everything is subject to change.


AUTHOR

Send bug reports, hints, tips, suggestions to Enno Derksen at <enno@att.com>.


SEE ALSO

The home page of XML::Checker at http://www.erols.com/enno/checker/index.html

The XML spec (Extensible Markup Language 1.0) at http://www.w3.org/TR/REC-xml

The the XML::Parser manpage and the XML::Parser::Expat manpage manual pages.

The other packages that come with XML::Checker: the XML::Checker::Parser manpage, the XML::DOM::ValParser manpage

The DOM Level 1 specification at http://www.w3.org/TR/REC-DOM-Level-1

The PerlSAX specification. It is currently in lib/PerlSAX.pod in the libxml-perl distribution by Ken MacLeod.

The original SAX specification (Simple API for XML) can be found at http://www.megginson.com/SAX and http://www.megginson.com/SAX/SAX2