Hi ,
Use the loadGrammar function of the XercesDOMParser class.
setExternalNoNamespaceSchemaLocation is not meant for validating the xml
instance. check it's documentation at
http://xml.apache.org/xerces-c/apiDocs/classAbstractDOMParser.html#z871_13
Thanks.
Regards,
Umesh
On Wed, 2007-04-18 at 12:35 +0100, martin waller wrote:
> Hi,
>
> I'm trying to work out how to validate xml files with a simple schema
> I've written and I'm clearly missing something.
>
> Here's the code that I'm using to do the validation:
>
> //initialisation stuff...
>
> XercesDOMParser parser;
> parser.setValidationScheme(XercesDOMParser::Val_Always);
> parser.setDoNamespaces(true);
> parser.setDoSchema(true);
>
> parser.setExternalNoNamespaceSchemaLocation(testschema.c_str());//the
> schema..
> try
> {
> parser.parse(testfile.c_str());
> }
> catch (const XMLException& toCatch) {
> char* message = XMLString::transcode(toCatch.getMessage());
> cout << "Exception message is: \n"
> << message << "\n";
> XMLString::release(&message);
> }
> catch (const DOMException& toCatch) {
> char* message = XMLString::transcode(toCatch.msg);
> cout << "Exception message is: \n"
> << message << "\n";
> XMLString::release(&message);
> }
> catch (...) {
> cout << "Unexpected Exception \n" ;
> }
>
> try
> {
> parser.parse(testfile2.c_str());
> }
> catch (const XMLException& toCatch) {
> char* message = XMLString::transcode(toCatch.getMessage());
> cout << "Exception message is: \n"
> << message << "\n";
> XMLString::release(&message);
> }
> catch (const DOMException& toCatch) {
> char* message = XMLString::transcode(toCatch.msg);
> cout << "Exception message is: \n"
> << message << "\n";
> XMLString::release(&message);
> }
> catch (...) {
> cout << "Unexpected Exception \n" ;
> }
>
> 'testfile' should be valid, and testfile2 isn't, but when I run the
> program 'nothing happens' (it runs, no exceptions, no complaints).
>
> Here's the xsd file:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> <xsd:element name="componentDefinition">
> <xsd:complexType>
> <xsd:all>
> <xsd:element name="refDes" type="xsd:string" minOccurs="0"
> maxOccurs="1"/>
> <xsd:element name="packageName" type="xsd:string"
> maxOccurs="1"/>
> <xsd:element name="partNumber" type="xsd:string"
> minOccurs="0" maxOccurs="1"/>
> <xsd:element name="power" type="xsd:float" maxOccurs="1"/>
> <xsd:element name="powerUnits" type="xsd:string"
> minOccurs="0" maxOccurs="1"/>
> <xsd:element name="zRot">
> <xsd:simpleType>
> <xsd:restriction base="xsd:string">
> <xsd:enumeration value="0"/>
> <xsd:enumeration value="90"/>
> <xsd:enumeration value="180"/>
> <xsd:enumeration value="270"/>
> </xsd:restriction>
> </xsd:simpleType>
> </xsd:element>
> <xsd:element name="length" type="xsd:float" maxOccurs="1"/>
> <xsd:element name="width" type="xsd:float" maxOccurs="1"/>
> <xsd:element name="lengthUnits" type="xsd:string"
> minOccurs="0" maxOccurs="1"/>
> <xsd:element name="height" type="xsd:float" maxOccurs="1"/>
> <xsd:element name="heightUnits" type="xsd:string"
> minOccurs="0" maxOccurs="1"/>
> </xsd:all>
> <xsd:element name="property" minOccurs="0" maxOccurs="unbounded">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element name="propName" type="xsd:string"/>
> <xsd:element name="propValue" type="xsd:string"/>
> <xsd:element name="propUnits" type="xsd:string"
> minOccurs="0"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
> </xsd:complexType>
> </xsd:element>
> </xsd:schema>
>
> Here's the invalid xml file testfile2):
> <?xml version="1.0" encoding="utf-8" ?>
> <componentDefinition xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
> xsi:noNamespaceSchemaLocation="CompDef.xsd">
> <refDes>U1</refDes>
> <rubbish>Test01-02-03</rubbish>
> <power>3</power>
> <powerUnits>W</powerUnits>
> <zRot>0</zRot>
> <length>100</length>
> <width>50</width>
> <lengthUnits>mm</lengthUnits>
> <height>4</height>
> <heightUnits>mm</heightUnits>
> </componentDefinition>
>
>
>
> I'm not quite sure what to expect when it parses the invalid file? It
> seems to do it OK then just returns normally. Shuld it throw an
> exception or what? I'm a bit overwhelmed by the docs...like I say, I'm
> new to this so please treat me as a beginner...
>
> Martin
>
>