Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by Plankton (Vicar)
on Sep 30, 2003 at 19:40 UTC ( [id://295416]=note: print w/replies, xml ) Need Help??


in reply to Re: Is there any way I can validate XML documents with XML Schema using Perl?
in thread Is there any way I can validate XML documents with XML Schema using Perl?

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 perl_xml_schema.pl #!/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="http://www.w3.org/2001/XMLSchema-instance" 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="http://www.w3.org/2001/XMLSchema"> <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$ ./perl_xml_schema.pl XML/invoice.xml XML/simpleInvoice.xsd Found element without a name. bash-2.03$ ls -l >junk bash-2.03$ ./perl_xml_schema.pl junk XML/simpleInvoice.xsd Found element without a name.
What's going on here?

Plankton: 1% Evil, 99% Hot Gas.

Replies are listed 'Best First'.
Re: Re: Re: Is there any way I can validate XML documents with XML Schema using Perl?
by samtregar (Abbot) on Sep 30, 2003 at 21:34 UTC
    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!

    -sam

        You're very welcome. Now get to work on Type.pm! ;)

        -sam

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://295416]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found