skip to content »

Validating xml with perl

Example 22-7 is a DTD for the element didn't follow the DTD, but it couldn't tell us precisely what it violated in the DTD or how.At the time of this writing, you must use XML:: Xerces to validate while using SAX, or to validate against W3C Schema.

validating xml with perl-54

Love it or hate it, HTML offered a standard, ubiquitous markup language that one could expect would be viewable as more or less intended by anyone requesting the document.That is, the of the markup content is handled implicitly by virtue of using an application that is specifically designed to render that markup in a predictable way.Choosing XML to markup web content knocks that implicit validation into a cocked hat.Content models may be open or closed; you can declare a document structurally valid based on a single all-important rule; or you can create rules for each and every element and attribute that may appear in the document -- the choice is yours.This month we will be looking at the Perl implementation of the Schematron: my element in the context of the abstract root.It uses the XPath syntax to define a series of rules that should or should not be true about a given document's structure.

Those rules, and the context in which they are evaluated, can be as coarse or as finely-grained as the task at hand requires.

Schematron's secret is that it's most often implemented as an XSLT stylesheet, in which the Schematron stylesheet is applied to the schema and the result of that transformation is applied as a stylesheet to the document being validated.

The same is true with most flavors of except that the stylesheet is created dynamically and all the details hidden from view.

Any valid XPath expression that can be evaluated as true or false can be used to test a document's structure. /usr/bin/perl -w use strict; use XML:: Schematron:: Lib XSLT; my $schema_file = $ARGV[0]; my $xml_file = $ARGV[1]; die "Usage: perl schemafile XMLfile.\n" unless defined $schema_file and defined $xml_file; my $tron = XML:: Schematron:: Lib XSLT-.

The reason is that the Schematron module actually ships with several backends that can be chosen based on the type of processor that you want to use.

However, the complete working application is available in this month's sample code for you to peruse, install, or extend as desired.