Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Is there any way I can validate XML documents with XML Schema using Perl?

by Plankton (Vicar)
on Sep 29, 2003 at 22:56 UTC ( #295140=perlquestion: print w/replies, xml ) Need Help??
Plankton has asked for the wisdom of the Perl Monks concerning the following question:


I have been trying to learn about XML / XML Schema. I wonder if any monk could help me out and show me how to validate a XML document against a XML Schema with Perl? I found this node but some of the links are no longer there.

Plankton: 1% Evil, 99% Hot Gas.
  • Comment on Is there any way I can validate XML documents with XML Schema using Perl?

Replies are listed 'Best First'.
Re: Is there any way I can validate XML documents with XML Schema using Perl?
by samtregar (Abbot) on Sep 29, 2003 at 23:02 UTC
    XML::Validator::Schema! It's at an early stage of development, but it can already handle a pretty useful chunk of the spec. If you find that it's nearly good enough but lacks something specific you need feel free to drop me a line.


    PS: I'm in the middle of reading XML Schema by Eric van der Vlist from O'Reilly. I recommend it if you're interested in learning to use XML Schema. It has a rather large number of typos, but the text is straight-forward and easy to follow. Unlike the spec!

      Thanks for the info samtregar maybe you will be kind enough to help me out somemore? Here's what i got going so far ...
      bash-2.03$ cat #!/usr/local/bin/perl -w use strict; use XML::SAX::ParserFactory; use XML::Validator::Schema; sub usage { return "$0 <xml> <xsd>\n"; } my $xml = shift || die usage; my $xsd = shift || die usage; # # create a new validator object, using foo.xsd # my $validator = XML::Validator::Schema->new(file => $xsd); # # create a SAX parser and assign the validator as a Handler # my $parser = XML::SAX::ParserFactory->parser(Handler => $validator); # # validate foo.xml against foo.xsd # eval { $parser->parse_uri($xml) }; die "File failed validation: $@" if + $@; bash-2.03$ cat XML/invoice.xml <?xml version="1.0"?> <invoice xmlns:xsi="" xsi:noNamespaceSchemaLocation="XML/simpleInvoice.xsd"> <invoiceNumber>A1112CD</invoiceNumber> <originator> <companyName>Metaphorical Web</companyName> <companyContact>James Eldridge</companyContact> <companyIdentifier>MetWeb</companyIdentifier> </originator> <receiver> <companyName>Semantic Web</companyName> <companyContact>Sarah Tremaine</companyContact> <companyIdentifier>SemanticWeb</companyIdentifier> </receiver> <lineItems> <lineItem> <itemDescription>Essay on Metaphorical Web</itemDescription> <itemCount>1</itemCount> <itemUnit>Article</itemUnit> <itemPrice currency="USD">155.60</itemPrice> <itemTotal currency="USD">155.60</itemTotal> </lineItem> <lineItem> <itemDescription>Lesson Package </itemDescription> <itemCount>4</itemCount> <itemUnit>Lesson</itemUnit> <itemPrice currency="USD">176.13</itemPrice> <itemTotal currency="USD">704.52</itemTotal> </lineItem> </lineItems> <total>860.12</total> </invoice> bash-2.03$ cat XML/simpleInvoice.xsd <!-- simpleInvoice.xsd --> <xsd:schema xmlns:xsd=""> <xsd:element name="invoiceNumber" type="xsd:string"> </xsd:element> <xsd:element name="originator"> <xsd:complexType> <xsd:sequence> <xsd:element ref="companyName"/> <xsd:element ref="companyContact"/> <xsd:element ref="companyIdentifier"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="companyName" type="xsd:string"> </xsd:element> <xsd:element name="companyContact" type="xsd:string"> </xsd:element> <xsd:element name="companyIdentifier" type="xsd:string"> </xsd:element> <xsd:element name="receiver"> <xsd:complexType> <xsd:sequence> <xsd:element ref="companyName"/> <xsd:element ref="companyContact"/> <xsd:element ref="companyIdentifier"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="itemDescription" type="xsd:string"> </xsd:element> <xsd:element name="itemCount" type="xsd:string"> </xsd:element> <xsd:element name="itemUnit" type="xsd:string"> </xsd:element> <xsd:element name="itemPrice"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="currency" type="xsd:string" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> <xsd:element name="itemTotal"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="currency" type="xsd:string" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> <xsd:element name="lineItem"> <xsd:complexType> <xsd:sequence> <xsd:element ref="itemDescription"/> <xsd:element ref="itemCount"/> <xsd:element ref="itemUnit"/> <xsd:element ref="itemPrice"/> <xsd:element ref="itemTotal"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="lineItems"> <xsd:complexType> <xsd:sequence maxOccurs="unbounded"> <xsd:element ref="lineItem"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="total" type="xsd:string"> </xsd:element> <xsd:element name="invoice"> <xsd:complexType> <xsd:sequence> <xsd:element ref="invoiceNumber"/> <xsd:element ref="originator"/> <xsd:element ref="receiver"/> <xsd:element ref="lineItems"/> <xsd:element ref="total"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
      Now when i run my script I get this ...
      bash-2.03$ ./ XML/invoice.xml XML/simpleInvoice.xsd Found element without a name. bash-2.03$ ls -l >junk bash-2.03$ ./ junk XML/simpleInvoice.xsd Found element without a name.
      What's going on here?

      Plankton: 1% Evil, 99% Hot Gas.
        What's going on here?

        I think you forgot to RTFM all the way through. Please consult the SCHEMA SUPPORT section in particular. As you'll read, you're using a number of constructs that are not yet supported by XML::Validator::Schema, including refs and multiple root elements. I think it should be possible to express your schema in terms that XML::Validator::Schema can understand, but it will be very deep and rather hard to read.

        I expect to eventually add support for all the constructs you're using. Of course, it will go much faster if I get some help. Patches welcome!


Re: Is there any way I can validate XML documents with XML Schema using Perl?
by Anonymous Monk on Sep 30, 2003 at 11:32 UTC
    Join the axkit-users mailing list, or just search the recent archives ( This question has been answered there. Not only that but the cream of XML Perl knowledge is pretty much all on that list...
      From the AxKit Install instructions:
      Installing and Configuring AxKit

      This guide presumes that you already have an Apache httpd server running mod_perl. If this is not the case, please visit the Apache and mod_perl pages for more information.

      I am not able to install Apache or mod_perl on the system I have to use.

      Plankton: 1% Evil, 99% Hot Gas.
        You don't need to install AxKit. A little googling provides the answer. Someone asked a question about schema validation on the axkit-user mailing list. The answer was that the next version of XML::LibXML will support XML Schema and RelaxNG validation. I also found an announcement on the perl-xml mailing list about this. Go look in the mailing list archives for more info.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://295140]
Approved by perrin
Front-paged by perrin
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2017-05-30 11:36 GMT
Find Nodes?
    Voting Booth?