From: Grant M. <gr...@us...> - 2002-04-04 21:35:26
|
Update of /cvsroot/perl-xml/perl-xml-faq In directory usw-pr-cvs1:/tmp/cvs-serv24884 Modified Files: perl-xml-faq.xml Log Message: - more stuff on validation Index: perl-xml-faq.xml =================================================================== RCS file: /cvsroot/perl-xml/perl-xml-faq/perl-xml-faq.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- perl-xml-faq.xml 2 Apr 2002 21:42:35 -0000 1.2 +++ perl-xml-faq.xml 4 Apr 2002 21:35:22 -0000 1.3 @@ -1274,9 +1274,51 @@ you would use <classname>XML::Parser</classname>. It works the same way except that it performs DTD validation.</para> -<!-- -FIXME: XML::Checker sample code ---> + <para>Here's a short example to get you going. Here's a DTD saved + in a file called <filename>/opt/xml/xcard.dtd</filename>:</para> + + <programlisting><![CDATA[ +<!ELEMENT xcard (firstname,lastname,email?)> +<!ELEMENT firstname (#PCDATA)> +<!ELEMENT lastname (#PCDATA)> +<!ELEMENT email (#PCDATA)> + ]]></programlisting> + + <para>Here's an XML document that refer's to the DTD:</para> + + <programlisting><![CDATA[ +<?xml version="1.0" ?> +<!DOCTYPE xcard SYSTEM "file:/opt/xml/xcard.dtd" > +<xcard> +<firstname>Joe</firstname> +<lastname>Bloggs</lastname> +<email>jo...@bl...</email> +</xcard> + ]]></programlisting> + + <para>And here's a code snippet to validate the document:</para> + + <programlisting><![CDATA[ +use XML::Checker::Parser; + +my $xp = new XML::Checker::Parser ( Handlers => { } ); + +eval { + $xp->parsefile($xml_file); +}; +if ($@) { + # ... your error handling code here ... + print "$xml_file failed validation!\n"; + die "$@"; +} +print "$xml_file passed validation\n"; + ]]></programlisting> + + <para>You can play around with adding and removing elements from the + document to get a idea of what happens when validation errors + occur. You'll also want to refer to the documentation for the + 'SkipExternalDTD' option for more robust handling of external + DTDs.</para> </answer> </qandaentry> @@ -1295,6 +1337,12 @@ $parser->validation(1); ]]></programlisting> + <para>Validation using <classname>XML::LibXML</classname> is much faster + than with <classname>XML::Checker</classname> but if you want to know why + a document fails validation you'll find that + <classname>XML::LibXML</classname>'s diagnostic messages are not as + helpful.</para> + </answer> </qandaentry> @@ -1308,6 +1356,21 @@ url="http://www.xml.com/pub/au/83">an article</ulink> describing how a combination of Perl and XPath can provide a quick, lightweight solution for validating documents.</para> + + </answer> + </qandaentry> + + <qandaentry id="validation_schematron"> + <question> + <para><classname>XML::Schematron</classname></para> + </question> + <answer> + + <para>Kip has also written the <classname>XML::Schematron</classname> + module which can be used with either <classname>XML::XPath</classname> or + <classname>XML::Sablotron</classname> to implement validation based on + Rick Jelliffe's <ulink + url="http://www.ascc.net/xml/resource/schematron/">Schematron</ulink>.</para> </answer> </qandaentry> |