Schema(3pm)
π NAME π
okay so bestie, welcome to Schema(3pm) β the User Contributed Perl Documentation page for XML::Validator::Schema, and it does exactly this: validate XML against a subset of W3C XML Schema. π
β¨ SYNOPSIS β¨
No cap, here's the whole vibe:
use XML::SAX::ParserFactory;
use XML::Validator::Schema;
#
# create a new validator object, using foo.xsd
#
$validator = XML::Validator::Schema->new(file => 'foo.xsd');
#
# create a SAX parser and assign the validator as a Handler
#
$parser = XML::SAX::ParserFactory->parser(Handler => $validator);
#
# validate foo.xml against foo.xsd
#
eval { $parser->parse_uri('foo.xml') };
die "File failed validation: $@" if $@;
πΈ DESCRIPTION πΈ
This module lets you validate XML documents against a W3C XML Schema. It does not implement the full W3C XML Schema recommendation (http://www.w3.org/XML/Schema), just a useful subset. Peep the SCHEMA SUPPORT section below for the tea. β
IMPORTANT NOTE: To get line and column numbers in the error messages generated by this module you must installXML::Filter::ExceptionLocatorand useXML::SAX::ExpatXSas your SAX parser. This module is much more useful if you can tell where your errors are, so using these modules is highly recommended! π¨
π INTERFACE π
XML::Validator::Schema->new(file => 'file.xsd', cache => 1)β call this method to create a newXML::Validator:Schemaobject. The only required option isfile, which must provide a path to an XML Schema document. periodt.
Setting the optional cache parameter to 1 causes XML::Validator::Schema to keep a copy of the schema parse tree in memory. The tree will be reused on subsequent calls with the same file parameter, as long as the mtime on the schema file hasn't changed. This can save a lot of time if you're validating many documents against a single schema. πΎ
Since XML::Validator::Schema is a SAX filter you will normally pass this object to a SAX parser:
$validator = XML::Validator::Schema->new(file => 'foo.xsd');
$parser = XML::SAX::ParserFactory->parser(Handler => $validator);
Then you can proceed to validate files using the parser:
eval { $parser->parse_uri('foo.xml') };
die "File failed validation: $@" if $@;
Setting the optional debug parameter to 1 causes XML::Validator::Schema to output elements and associated attributes while parsing and validating the XML document. This provides useful information on the position where the validation failed (although not as useful as the line and column numbers included when XML::Filter::ExceptiionLocator and XML::SAX::ExpatXS are used). π
π RATIONALE π
okay so the author is writing a piece of software which uses Xerces/C++ (http://xml.apache.org/xerces-c/) to validate documents against XML Schema schemas. This works very well, but they'd like to release their project to the world. Requiring users to install Xerces is simply too onerous a requirement; few will have it already and the Xerces installation system leaves much to be desired. π
On CPAN, the only available XML Schema validator is XML::Schema. Unfortunately, this module isn't ready for use as it lacks the ability to actually parse the XML Schema document format! The author looked into enhancing XML::Schema but must admit that they're not smart enough to understand the code... One day, when XML::Schema is completed, they will replace this module with a wrapper around it. π«Ά
This module represents the author's attempt to support enough XML Schema syntax to be useful without attempting to tackle the full standard. They're sure this will mean that it can't be used in all situations, but hopefully that won't prevent it from being used at all. β¨
π SCHEMA SUPPORT π
π¦ Supported Elements π¦
The following elements are supported by the XML Schema parser. If you don't see an element or an attribute here then you definitely can't use it in a schema document. π
You can expect that the schema document parser will produce an error if you include elements which are not supported. However, unsupported attributes may be silently ignored. This should not be misconstrued as a feature and will eventually be fixed. π§
All of these elements must be in the http://www.w3.org/2001/XMLSchema namespace, either using a default namespace or a prefix. π§
<schema>- Supported attributes:
targetNamespace,elementFormDefault,attributeFormDefault - Notes: the only supported values for
elementFormDefaultandattributeFormDefaultare"unqualified."As such,targetNamespaceis essentially ignored.
- Supported attributes:
<element name="foo">- Supported attributes:
name,type,minOccurs,maxOccurs,ref
- Supported attributes:
<attribute>- Supported attributes:
name,type,use,ref
- Supported attributes:
<sequence>- Supported attributes:
minOccurs,maxOccurs
- Supported attributes:
<choice>- Supported attributes:
minOccurs,maxOccurs
- Supported attributes:
<all>- Supported attributes:
minOccurs,maxOccurs
- Supported attributes:
<complexType>- Supported attributes:
name
- Supported attributes:
<simpleContent>- The only supported sub-element is
<extension>.
- The only supported sub-element is
<extension>- Supported attributes:
base - Notes: only allowed inside
<simpleContent>
- Supported attributes:
<simpleType>- Supported attributes:
name
- Supported attributes:
<restriction>- Supported attributes:
base - Notes: only allowed inside
<simpleType>
- Supported attributes:
<whiteSpace>- Supported attributes:
value
- Supported attributes:
<pattern>- Supported attributes:
value
- Supported attributes:
<enumeration>- Supported attributes:
value
- Supported attributes:
<length>- Supported attributes:
value
- Supported attributes:
<minLength>- Supported attributes:
value
- Supported attributes:
<maxLength>- Supported attributes:
value
- Supported attributes:
<minInclusive>- Supported attributes:
value
- Supported attributes:
<minExclusive>- Supported attributes:
value
- Supported attributes:
<maxInclusive>- Supported attributes:
value
- Supported attributes:
<maxExclusive>- Supported attributes:
value
- Supported attributes:
<totalDigits>- Supported attributes:
value
- Supported attributes:
<fractionDigits>- Supported attributes:
value
- Supported attributes:
<annotation><documentation>- Supported attributes:
name
- Supported attributes:
<union>- Supported attributes:
MemberTypes
- Supported attributes:
πΈ Simple Type Support πΈ
Supported built-in types are: β¨
stringnormalizedStringtokenNMTOKEN- Notes: the spec says
NMTOKENshould only be used for attributes, but this rule is not enforced.
- Notes: the spec says
booleandecimal- Notes: the enumeration facet is not supported on
decimalor any types derived fromdecimal.
- Notes: the enumeration facet is not supported on
integerintshortbyteunsignedIntunsignedShortunsignedBytepositiveIntegernegativeIntegernonPositiveIntegernonNegativeIntegerdateTime- Notes: Although
dateTimecorrectly validates the lexical format it does not offer comparison facets (min*,max*,enumeration).
- Notes: Although
double- Notes: Although
doublecorrectly validates the lexical format it does not offer comparison facets (min*,max*,enumeration). Also, minimum and maximum constraints as described in the spec are not checked.
- Notes: Although
float- Notes: The restrictions on
doublesupport apply tofloatas well.
- Notes: The restrictions on
durationtimedategYearMonthgYeargMonthDaygDaygMonthhexBinarybase64BinaryanyURIQNameNOTATION
π Miscellaneous Details π
Other known deviations from the specification: π§
- Patterns specified in
patternsimpleType restrictions are Perl regexes with none of the XML Schema extensions available. - No effort is made to prevent the declaration of facets which "loosen" the restrictions on a type. This is a bug and will be fixed in a future release. Until then types which attempt to loosen restrictions on their base class will behave unpredictably. β οΈ
- No attempt has been made to exclude content models which are ambiguous, as the spec demands. In fact, the author doesn't see any compelling reason to do so, aside from strict compliance to the spec. The content model implementation uses regular expressions which should be able to handle loads of ambiguity without significant performance problems.
- Marking a facet "fixed" has no effect.
- SimpleTypes must come after their base types in the schema body. For example, this is ok:
<xs:simpleType name="foo">
<xs:restriction base="xs:string">
<xs:minLength value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="foo_bar">
<xs:restriction base="foo">
<xs:length value="10"/>
</xs:restriction>
</xs:simpleType>
But this is not:
<xs:simpleType name="foo_bar">
<xs:restriction base="foo">
<xs:length value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="foo">
<xs:restriction base="xs:string">
<xs:minLength value="10"/>
</xs:restriction>
</xs:simpleType>
π¨ CAVEATS π¨
Here are a few gotchas that you should know about: π
- No Unicode testing has been performed, although it seems possible that the module will handle Unicode data correctly.
- Namespace processing is almost entirely missing from the module.
- Little work has been done to ensure that invalid schemas fail gracefully. Until that is done you may want to develop your schemas using a more mature validator (like Xerces or XML Spy) before using them with this module.
π BUGS π
Please use rt.cpan.org to report bugs in this module:
http://rt.cpan.org
Please note that the author will delete bugs which merely point out the lack of support for a particular feature of XML Schema. Those are feature requests, and believe me, they know we've got a long way to go. π
π«Ά SUPPORT π«Ά
This module is supported on the perl-xml mailing-list. Please join the list if you have questions, suggestions or patches:
http://listserv.activestate.com/mailman/listinfo/perl-xml
πΎ CVS πΎ
If you'd like to help develop XML::Validator::Schema you'll want to check out a copy of the CVS tree:
http://sourceforge.net/cvs/?group_id=89764
π CREDITS π
The following people have contributed bug reports, test cases and/or code:
- Russell B Cecala (aka Plankton)
- David Wheeler
- Toby Long-Leather
- Mathieu
- h.bridge@fasol.fujitsu.com
- michael.jacob@schering.de
- josef@clubphoto.com
- adamk@ali.as
- Jean Flouret
β¨ AUTHOR β¨
Sam Tregar <sam@tregar.com>
π COPYRIGHT AND LICENSE π
So the copyright tea? It's Copyright (C) 2002-2003 Sam Tregar β periodt. This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself. Slay, it's open source. π
β¨ A NOTE ON DEVELOPMENT METHODOLOGY β¨
Okay so bestie, real talk: this module isn't just an XML Schema validator, it's also a whole test of the Test Driven Development methodology. It's giving two jobs, one icon. π
I've been writing tests while I develop code for a while now, but TDD goes further by requiring tests to be written before code. And the way that changes everything? One consequence is that the module code may seem naive; it really is just enough code to pass the current test suite. If I'm doing it right then there shouldn't be a single line of code that isn't directly related to passing a test. As I add functionality (by way of writing tests) I'll refactor the code a great deal, but I won't add code only to support future development. No cap, that's the whole vibe. π¦
For more information I recommend "Test Driven Development: By Example" by Kent Beck. Obsessed. πβ¨
π SEE ALSO π
This page is from perl v5.32.0, dated 2021-01-05, and it's Schema(3pm) β bye bestie, go forth and validate. π π
π¦ where this page comes from
- debian bookworm: libxml-validator-schema-perl 1.10-2.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-validator-schema-perl 1.10-2.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libxml-validator-schema-perl 1.10-2.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-validator-schema-perl 1.10-2.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-validator-schema-perl 1.10-2.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-validator-schema-perl 1.10-2.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms