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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-03-28 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found