The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

XML::Compile::Schema::Translate - create an XML data parser

INHERITANCE

 XML::Compile::Schema::Translate
   is an Exporter

SYNOPSIS

 # for internal use only!

DESCRIPTION

This module converts a schema type definition into a code reference which can be used to interpret a schema. The sole public function in this package is compile_tree(), and is called by XML::Compile::Schema::compile(), which does a lot of set-ups. Please do not try to use this package directly!

The code in this package interprets schemas; it understands, for instance, how complexType definitions work. Then, when the schema syntax is decoded, it will knot the pieces together into one CODE reference which can be used in the main user program.

Unsupported features

This implementation is work in progress, but most structures in W3C schema's are implemented. A few nuts are still to crack: schema elementFormDefault schema attributeFormDefault element default element mixed facets on dates limited understanding of patterns include some name-space features

Of course, these are all fixed in next release ;-)

Rules of translation

The following rules are used during translation, applicable to both the reader as the writer:

Encoding

this module uses XML::LibXML which does the character encoding for us: you do not have to escape characters like < yourself.

Nesting

Elements can be complex, and themselve contain elements which are complex. In the Perl representation of the data, this will be shown as nested hashes with the same structure as the XML.

Arrays

Any element which has a maxOccurs larger than 1 will be returned as an array (or undef). This will avoid the situation where the user code only handles a single element instance where the schema defines that multiple values can be returned. The same is true for list types.

Schema validation

Be warned that the schema itself is NOT VALIDATED; you can easily construct schema's which do work with this module, but are not valid according to W3C. Only in some cases, the translater will refuse to accept mistakes: mainly because it cannot produce valid code.

Value checking

The code will do its best to produce a correct translation. For instance, an accidental 1.9999 will be converted into 2 when the schema says that the field is an int.

Performance optimization

The XML::Compile::Schema::compile() method (and wrappers) defines a set options to improve performance or usability. These options are translated into the executed code: compile time, not run-time!

The following options with their implications:

sloppy_integers BOOLEAN

The integer type, as defined by the schema built-in specification, accepts really huge values. Also the derived types, like nonNegativeInteger can contain much larger values than Perl's internal long. Therefore, the module will start to use Math::BigInt for these types if needed.

However, in most cases, people design integer where an int suffices. The use of big-int values comes with heigh performance costs. Set this option to true when you are sure that ALL USES of integer in the scheme will fit into signed longs (are between -2147483648 and 2147483647 inclusive)

check_occurs BOOLEAN

Checking whether the number of occurrences for an item are between minOccurs and maxOccurs (implied for all, sequence, and choice or explictly specified) takes time. Of course, in cases errors must be handled. When this option is set to false, only distinction between single and array elements is made.

ignore_facets BOOLEAN

Facets limit field content in the restriction block of a simpleType. When this option is true, no checks are performed on the values. In some cases, this may cause problems: especially with whiteSpace and digits of floats. However, you may be able to control this yourself. In most cases, luck even plays a part in this. Less checks means a better performance.

Simple type restrictions are not implemented by other XML perl modules. When the schema is nicely detailed, this will give extra security.

Name-spaces

The translator does respect name-spaces, but not all senders and receivers of XML are name-space capable. Therefore, you have some options to interfere.

output_namespaces HASH

The translator will create XML elements (WRITER) which use name-spaces, based on its own name-space/prefix mapping administration. This is needed because the XML tree is formed bottom-up, where XML::LibXML can only handle this top-down.

When your pass your own HASH as argument, you can explicitly specify the prefixes you like to be used for which name-space. Found name-spaces will be added to the hash, as well the use count. When a new name-space URI is discovered, an attempt is made to use the prefix as found in the schema. Prefix collisions are actively avoided: when two URIs want the same prefix, a sequence number is added to one of them which makes it unique.

include_namespaces BOOLEAN

When true and WRITER, the top level returned XML element will contain the prefix definitions. Only name-spaces which are actually used will be included (a count is kept by the translator). It may very well list name-spaces which are not in the actual output because the fields which require them are not included for there is not value for those fields.

If you like to combine XML output from separate translated parts (for instance in case of generating SOAP), you may want to delay the inclusion of name-spaces until a higher level of the XML hierarchy which is produced later.

namespace_reset BOOLEAN

You can pass the same HASH to a next call to a reader or writer to get consistent name-space usage. However, when include_namespaces is used, you may get ghost name-space listings. This option will reset the counts on all defined name-spaces.

ignore_namespaces BOOLEAN

Do not use prefixes in the output XML for WRITER when false. For instance, because the destination application does not understand name-spaces. The include_namespaces will now also default to false

FUNCTIONS

XML::Compile::Schema::Translate->compile_tree(TYPENAME, OPTIONS)

    Do not call this function yourself, but use XML::Compile::Schema::compile() (or wrappers around that).

    This function returns a CODE reference, which can translate between Perl datastructures and XML, based on a schema. Before this method is called is the schema already translated into a table of types.

REFERENCES

See the XML::Compile website at http://perl.overmeer.net/xml-compile/ for more details.

COPYRIGHTS

Module version 0.01. Written by Mark Overmeer (mark@overmeer.net). See the ChangeLog for other contributors.

Copyright (c) 2006 by the author(s). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.