You are viewing a plain text version of this content. The canonical link for it is here.
Posted to p-dev@xerces.apache.org by Peter Moore <pm...@umn.edu> on 2003/05/09 22:30:06 UTC

W3C schema validation

I'm trying to validate some XML against an W3C Schema using xerces-p. 
Looking at a few examples I've found online I put together the 
following code, but I'm not getting the results I expect (some kind of 
error or warning) when the XML is obviously not conforming to the 
schema.

Can anyone tell me what I'm doing wrong?

use XML::Xerces;
my $p = XML::Xerces::XercesDOMParser->new();
$p->setDoNamespaces(1);
$p->setDoSchema (1);

my $error_handler = XML::Xerces::PerlErrorHandler->new();
$p->setErrorHandler($error_handler);

$p->setExternalSchemaLocation("http://somewhere.org/xxx.namespace 
http://somewhere.org/xxx.xsd");
$p->parse("test.xml");

Thanks.
- Peter


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-p-dev-help@xml.apache.org


Re: W3C schema validation

Posted by Peter Moore <pm...@umn.edu>.
Thanks for the reply Jason. Here is a simple example:

## test.xsd ##
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="foo">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="bar" type="xs:string"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
</xs:schema>

## test.xml ##
<?xml version="1.0"?>
<foo>
   <bar>xxx</bar>
   <bar>yyy</bar>
   <bad>zzz</bad>
</foo>

## validate.pl ##
use XML::Xerces;
my $p = XML::Xerces::XercesDOMParser->new();
$p->setDoNamespaces(1);
$p->setDoSchema (1);

my $error_handler = XML::Xerces::PerlErrorHandler->new();
$p->setErrorHandler($error_handler);

$p->setExternalNoNamespaceSchemaLocation("test.xsd");
$p->parse("test.xml");


I expect the validation to fail for two reasons: 1) There should only 
be a single <bar> element and 2) It should gag on the <bad> element. 
When I run the script it simply returns with no errors. I do however 
get errors when either the XML or XSD is not well-formed, so I know 
they are being parsed and that the error handler is working.

Any ideas? Thanks again.

-Peter

On Saturday, May 10, 2003, at 11:42 PM, Jason E. Stewart wrote:

> "Peter Moore" <pm...@umn.edu> writes:
>
>> I'm trying to validate some XML against an W3C Schema using
>> xerces-p. Looking at a few examples I've found online I put together
>> the following code, but I'm not getting the results I expect (some
>> kind of error or warning) when the XML is obviously not conforming to
>> the schema.
>>
>>
>> Can anyone tell me what I'm doing wrong?
>>
>> use XML::Xerces;
>> my $p = XML::Xerces::XercesDOMParser->new();
>> $p->setDoNamespaces(1);
>> $p->setDoSchema (1);
>>
>> my $error_handler = XML::Xerces::PerlErrorHandler->new();
>> $p->setErrorHandler($error_handler);
>>
>> $p->setExternalSchemaLocation("http://somewhere.org/xxx.namespace
>> http://somewhere.org/xxx.xsd");
>>
>> $p->parse("test.xml");
>
> Hi Peter,
>
> The code looks correct - schema support under XML::Xerces is not well
> tested - it *should* be working flawlessly since I've done nothing
> special except hooking it up to the underlying C++ code. But I haven't
> been using W3C schemas in my own work and so I haven't tested the
> support.
>
> Perhaps if you post a document and schema that should cause errors I
> can work on it?
>
> Cheers,
> jas.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-p-dev-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-p-dev-help@xml.apache.org


Re: W3C schema validation

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Peter Moore" <pm...@umn.edu> writes:

> I'm trying to validate some XML against an W3C Schema using
> xerces-p. Looking at a few examples I've found online I put together
> the following code, but I'm not getting the results I expect (some
> kind of error or warning) when the XML is obviously not conforming to
> the schema.
> 
> 
> Can anyone tell me what I'm doing wrong?
> 
> use XML::Xerces;
> my $p = XML::Xerces::XercesDOMParser->new();
> $p->setDoNamespaces(1);
> $p->setDoSchema (1);
> 
> my $error_handler = XML::Xerces::PerlErrorHandler->new();
> $p->setErrorHandler($error_handler);
> 
> $p->setExternalSchemaLocation("http://somewhere.org/xxx.namespace
> http://somewhere.org/xxx.xsd");
> 
> $p->parse("test.xml");

Hi Peter,

The code looks correct - schema support under XML::Xerces is not well
tested - it *should* be working flawlessly since I've done nothing
special except hooking it up to the underlying C++ code. But I haven't
been using W3C schemas in my own work and so I haven't tested the
support. 

Perhaps if you post a document and schema that should cause errors I
can work on it?

Cheers,
jas.

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-p-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-p-dev-help@xml.apache.org