XML::Schematron::XPath - Perl extension for validating XML with XPath expressions. |
XML::Schematron::XPath - Perl extension for validating XML with XPath expressions.
use XML::Schematron my $pseudotron = XML::Schematron->new_with_traits( traits => ['XPath'], schema => 'my_schema.xml' ); # optionally, add some addition tests from perl-space $pseudotron->add_tests( @some_tests ); my $messages = $pseudotron->verify('my_file.xml');
if ($messages) { # we got warnings or errors during validation... ... } OR, in an array context
my @messages = $pseudotron->verify('my_file.xml');
XML::Schematron::XPath serves as a simple validator for XML based on Rick JELLIFFE's Schematron XSLT script. A Schematron schema defines a set of rules in the XPath language that are used to examine the contents of an XML document tree.
A simplified example: <?xml version=``1.0''?> <schema> <pattern> <rule context=``page''> <assert test=``count(*)=count(title|body)''>The page element may only contain title or body elements.</assert> <assert test=``@name''>A page element must contain a name attribute.</assert> <report test=``string-length(@name) < 5''>A page element name attribute must be at least 5 characters long.</report> </rule> </pattern> </schema>
Note that an 'assert' rule will return if the result of the test expression is not true, while a 'report' rule will return only if the test expression evalutes to true.
add_test()
method allows you push additional a additional test on to the stack before validation. This method's argument must be an XML::Schematron::Test object or a hash reference with the following structure:
Arguments for this method:
Example:
$obj->add_test({expr => 'count(@*) > 0', context => '/pattern', message => 'Pattern should have at least one attribute', type => 'assert', pattern => 'Basic tests'});
Note that add_test()
pushes a new test on to the existing test list, while tests()
redefines the entire list.
add_tests()
method allows you push an additional list of tests on to the stack before validation. Each element must be an XML::Schematron::Test object or a hash reference. See above for the list of key/value pairs expected if hashrefs are used.
verify()
method takes the path to the XML document that you wish to validate, or a scalar containing the entire document
as a string, as its sole argument. It returns the messages that are returned during validation. When called in an array
context, this method returns an array of the messages generated during validation. When called in a scalar context, this
method returns a concatenated string of all output.
XML::Schematron::XPath does not conform to the current Schematron specification since more modern versions allow XSLT-specific expressions to be used as tests. Please note, however, that robust validation is still quite possible using just the XPath language.
Kip Hampton, khampton@totalcinema.com
Copyright (c) 2000-2010 Kip Hampton. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
For information about Schematron, sample schemas, and tutorials to help you write your own schmemas, please visit the Schematron homepage at: http://www.ascc.net/xml/resource/schematron/
For detailed information about the XPath syntax, please see the W3C XPath Specification at: http://www.w3.org/TR/xpath.html
XML::Schematron::XPath - Perl extension for validating XML with XPath expressions. |