Re: validate an XML against an XSD
Stephen Collyer <[email protected]> Thu, 22 Sep 2005 14:31:17 +0100
| Newsgroups | gmane.text.xml.xerces-p.devel |
|---|---|
| Message-ID | <[email protected]> |
Raju Sunny wrote:
> Hei sorry for the completely foolish question..
Isn't this the same question ?
> i need to validate an XML file against an xsd in perl
> IS there any way for doing that using xerces c
Create a parser like this:
> my $parser = XML::Xerces::XMLReaderFactory::createXMLReader();
>
> $parser->setFeature("$XML::Xerces::XMLUni::fgSAX2CoreNameSpaces", 1);
>
> $parser->setFeature("$XML::Xerces::XMLUni::fgSAX2CoreValidation", 1);
> $parser->setFeature("$XML::Xerces::XMLUni::fgXercesSchema", 1);
This creates a parser with the features required for schema validation.
Then feed this parser XML which specifies a schemaLocation in the
root element like e.g.
> <myelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://my.schema.loc/schema file:/schemas/s1.xsd">
You'll have to be prepared to catch and handle validation exceptions
in the code:
> eval
> {
> $parser->parse(XML::Xerces::MemBufInputSource->new($args{xml}));
> };
> if ($@)
> {
AFAIK, Xerces-C does not allow you to override the schema specified
by the XML document via the fgXercesSchemaExternalSchemaLocation
feature i.e. you can't create a parser and tell it always to
validate against a specific schema; you have to live with what's
specified in the document.
--
Regards
Stephen Collyer
Netspinner Ltd