From: Grant M. <gr...@us...> - 2005-11-02 09:03:20
|
Update of /cvsroot/perl-xml/perl-xml-faq In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4540 Modified Files: perl-xml-faq.xml Log Message: - integrate tip from Dan Horne re schema validation using XML::LibXML Index: perl-xml-faq.xml =================================================================== RCS file: /cvsroot/perl-xml/perl-xml-faq/perl-xml-faq.xml,v retrieving revision 1.20 retrieving revision 1.21 diff -u -d -r1.20 -r1.21 --- perl-xml-faq.xml 11 Feb 2005 08:06:16 -0000 1.20 +++ perl-xml-faq.xml 2 Nov 2005 09:03:03 -0000 1.21 @@ -1829,6 +1829,48 @@ </answer> </qandaentry> + <qandaentry id="validation_libxml_xsd"> + <question> + <para>W3C Schema Validation With <classname>XML::LibXML</classname></para> + </question> + <answer> + + <para><classname>XML::LibXML</classname> provides undocumented support + for validating against a W3C schema. Here's an example of how you might + use it (contributed by Dan Horne):</para> + + <programlisting><![CDATA[ +use XML::LibXML; + +my $schema_file = 'po.xsd'; +my $document = 'po.xml'; + +my $schema = XML::LibXML::Schema->new(location => $schema_file); + +my $parser = XML::LibXML->new; +my $doc = $parser->parse_file($document); + +eval { $schema->validate($doc) }; +die $@ if $@; + +print "$document validated successfully\n"; + ]]></programlisting> + + <para>The referenced XSD schema file and sample XML document can be + downloaded from the <ulink url="http://www.w3.org/TR/xmlschema-0/">W3C + Schema Primer</ulink>.</para> + + <para>The xmllint command line validator included in the libxml2 + distribution can also do W3C schema validation. You can use it like + this:</para> + + <programlisting><![CDATA[ +xmllint --noout --schema po.xsd po.xml + ]]></programlisting> + + </answer> + </qandaentry> + <qandaentry id="validation_xerces"> <question> <para>W3C Schema Validation With <classname>XML::Xerces</classname></para> @@ -1852,7 +1894,24 @@ <para>Sam Tregar's <classname>XML::Validator::Schema</classname> allows you to validate XML documents against a W3C XML Schema. It does not implement the full W3C XML Schema recommendation, but a useful - subset.</para> + subset. Here's an example:</para> + + <programlisting><![CDATA[ +use XML::SAX::ParserFactory; +use XML::Validator::Schema; + +my $schema_file = 'po.xsd'; +my $document = 'po.xml'; + +my $validator = XML::Validator::Schema->new(file => $schema_file); + +my $parser = XML::SAX::ParserFactory->parser(Handler => $validator); + +eval { $parser->parse_uri($document); }; +die $@ if $@; + +print "$document validated successfully\n"; + ]]></programlisting> </answer> </qandaentry> |