Well, I didn't learn more ...
Here is the code I use:
XercesDOMParser* parser = new XercesDOMParser();
parser->setDoValidation(true);
parser->setDoSchema(true);
parser->setDoNamespaces(true);
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setExternalSchemaLocation ( "http://www.elodig.fr/logs
file:///path/schema.xsd" );
try
{
parser->parse ( xmlFile );
}
catch (const OutOfMemoryException& toCatch )
{
...
And I don't catch any error (see the xml and xsd file at the bottom :
there are errors in the xml file).
I tried also to load the grammar:
parser->useCachedGrammarInParse(true);
parser->useCachedGrammarInParse(true);
parser->loadGrammar("/path/shcema.xsd", Grammar::SchemaGrammarType, true);
Without any success either.
Here are the xsd file and then the xml one :
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.elodig.fr/logs"
xmlns="http://www.elodig.fr/logs" elementFormDefault="qualified">
<!-- Defines log data elements -->
<xs:element name="data_element">
<xs:complexType>
<xs:sequence>
<xs:element name="element_name">
<xs:simpleType>
<xs:restriction
base="xs:string">
<xs:minLength
value="0"/>
<xs:maxLength
value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="element_value">
<xs:simpleType>
<xs:restriction
base="xs:string">
<xs:minLength
value="0"/>
<xs:maxLength
value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Defines a log -->
<xs:element name="log">
<xs:complexType>
<xs:sequence>
<xs:element name="id"
type="xs:nonNegativeInteger" />
<xs:element name="extension">
<xs:simpleType>
<xs:restriction
base="xs:string">
<xs:minLength
value="0"/>
<xs:maxLength
value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="event_type">
<xs:simpleType>
<xs:restriction
base="xs:string">
<xs:enumeration
value="action" />
<xs:enumeration
value="command" />
<xs:enumeration
value="result" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element
ref="data_element" />
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="record_time"
type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Defines a set of logs -->
<xs:element name="logs">
<xs:complexType>
<xs:sequence>
<xs:element ref="log" />
<xs:any minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<log
xmlns="http://www.elodig.fr/logs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.elodig.fr/logs file:///path/schema.xsd">
<id>1</id>
<bubu></bubu>
<extension>base</extension>
<event_type>action</event_type>
<data type="bubu">
<data_element>
<element_name>bibi</element_name>
<element_value>is happy</element_value>
</data_element>
<data_element>
<element_name>baba</element_name>
<element_value>is not happy</element_value>
</data_element>
</data>
</log>
According to the xsd file, everywhere where "bubu" appears should
trigger an exception. It does not.
Any clue?
Thanks,
- Emmanuel
Jesse Pelton wrote:
> You may also want to take a look at DOMPrint. It's roughly equivalent
> to SAX2Print but uses DOM APIs instead of SAX 2.
>
> -----Original Message-----
> From: David Bertoni [mailto:dbertoni@xxxxxxxxxx]
> Sent: Monday, April 30, 2007 2:38 PM
> To: c-users@xxxxxxxxxxxxxxxxx
> Subject: Re: Hints on registering an XMLEntityResolver handler
>
> Emmanuel Guiton wrote:
>> Hello,
>>
>> I want to force an XML document validation using an XML Schema.
>> I am trying to register an XMLEntityResolver handler to do that (Iam
>> suing a DOMParser). Quite difficult for a beginner like me since the
>> documentation on http://xml.apache.org/xerces-c/apiDocs is wrong.
>
> The C++ documentation is generated from the source code using Doxygen,
> so I
> don't know what you mean by saying it's "wrong." Is it out-of-date?
> Does
> it not answer your questions? It's always helpful to know what's wrong,
> so
> we can fix it.
>
>> Does someone have good hints (a webpage with an example?) on how to
>> register an XMLEntityResolver handler?
>
> You cannot force validation using an XMLEntityResolver, although you can
>
> force the parser to use a particular schema document or documents. Can
> you
> describe exactly what you're doing? For example, users often forget to
> set
> all of the options necessary for schema validation.
>
>> Or does someone have a simpler way to validate an XML file against an
>> XML schema (which is stored on my local filesystem)? I've tried using
>> setExternalSchemaLocation without any success.
>
> As long as the document element contains a reference to the schema
> location
> hint you provided, and the proper options are set, it should work. Have
>
> you taken a look at the sample applications to see how to enable
> validation? SAX2Print is a good place to start.
>
> Another option for forcing validation against a particular schema is to
> use
> the proprietary loadGrammar() API. You can then set the feature to use
> the
> cached grammar during parsing:
>
> http://xml.apache.org/xerces-c/program-sax2.html#use-cached
>
> Dave
>
>
--
Directeur
Elodig SARL
30-32, avenue de la République
94800 Villejuif
Tel: 01 46 77 47 68
E-mail: emmanuel.guiton@xxxxxxxxx
http://www.elodig.fr
|