You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by evangeline <ev...@gmail.com> on 2009/05/02 10:18:24 UTC

Re: validating xml with xsd schema

So, I should declare a class that looks like this:

class XMLValidateError : public HandlerBase {
  public:
    XMLValidateError();
    void error(XMLException &e);

};

XMLValidateError::XMLValidateError() {

}

void XMLValidateError::error(XMLException &e) {
        char *message = XMLString::transcode(e.getMessage());
        qDebug() << "XML Error is: " << message << endl;
}


and then:
XMLValidateError *err_handler = new XMLValidateError();

But I still don't get any error messages and it still always returns
true...any thoughts ?


-- 
View this message in context: http://www.nabble.com/validating-xml-with-xsd-schema-tp23315036p23344077.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: validating xml with xsd schema

Posted by evangeline <ev...@gmail.com>.
Hi. Your suggestion works perfectly...but I still have one more question.

How can I pass the whole already read schema (char *) to X function, instead
of passing the filename to setExternalNoNamespaceSchemaLocation(filename)...

So what I want to know is the name of X function I can use.

Thanks
-- 
View this message in context: http://www.nabble.com/validating-xml-with-xsd-schema-tp23315036p23364984.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: validating xml with xsd schema

Posted by David Bertoni <db...@apache.org>.
Leif Goodwin wrote:
> We've tried the above code and it does not validate the XML file. All it does
> is check that it is well formed. Here is the hacked code: 
> 
>     /* initialize xerces system before usig API: DOM, SAX, SAX2 */ 
>     try { 
>         xercesc::XMLPlatformUtils::Initialize(); 
>     } 
>     catch (const XMLException &e) { 
>         char *message = XMLString::transcode(e.getMessage()); 
>         //qDebug() << "Error during XML initialization: " << message <<
> endl; 
>         XMLString::release(&message); 
>         //return; 
>     } 
> 
>     /* actual xerces work */ 
>     char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and Form
> Templates\\DrillData_1.xml"; 
>     char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and Form
> Templates\\newDrillData.xsd"; 
These are not valid URLs, because URLS cannot contain the space 
character, nor the '\' character. Instead, put these files in a 
directory with no space, and reference to them with legal URLs:

file:///Development/Chronicle/Example/newDrillData.xsd

>     SAXParser *parser = new SAXParser(); 
I would recommend you use a SAX2XMLReader, rather than a SAXParser.

> 
>     CSaxParserErrorHandler errorHandler;
>     parser->setErrorHandler(&errorHandler);
> 
>     //parser->setDoValidation(true); 
You have this commented out, so why would expect validation to occur?

>     parser->setDoNamespaces(true); 
You also need to call parser->setDoSchema(true).

> 
>     try 
>     { 
>         parser->setExternalSchemaLocation(xml_schema); 
As Alberto pointed out, this function does not accept a URL, it accepts 
a URI/URL pair.  The first is the namespace URI for the schema.  The 
second is the URL for the schema document itself.  See here for more 
information:

http://www.w3.org/TR/xmlschema-0/#ref40

If the schema does not use a namespace, then call 
setExternalNoNamespaceSchemaLocation() with just the URL for the schema 
document.

I would suggest you try running the SAX2Print sample application in the 
debugger and see what that code does.

Dave

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


Re: validating xml with xsd schema

Posted by Leif Goodwin <lg...@qgsl.com>.
Alberto: Thankyou very much. The code now works, and correctly flags up
validation errors in the XML. Much appreciated. (We have spent a lot of time
chasing this, so it is a relief to finally get it working.) 

Leif


Alberto Massari wrote:
> 
> Hi Leif,
> there is an extra setting missing:
> 
>     parser->setValidationScheme(SAXParser::Val_Auto); // or Val_Always, 
> if you prefer
> 
> Alberto
> 
> Leif Goodwin ha scritto:
>> Alberto. Apologies for missing your comment. (Easily done since they were
>> embedded in long quotes.) I corrected the code and found that the
>> setExternalSchemaLocation method cannot cope with folder names that
>> contain
>> whitespace. Moving the schema to a new file location, or replacing each
>> space with %20, and trying the new code, it still does not work. The
>> extra
>> illegal child element in the root element is not picked up as an error.
>> Incidentally we use a schema with a targetNamespace. 
>>
>> Test XML file: 
>>
>> <?xml version="1.0"?>
>> <driller-data 
>> 	xmlns="http://tempuri.org/DrillerData_1.0.xsd"
>> 	date="8 July 2009" drill-unit="Mickey Mouse"  driller = "Joe Smith"
>> shot-point-number="1" drill-depth="10.5" drill-type="conventional">
>> <dummy test="4"/>
>> </driller-data>
>>
>> Test schema file: 
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xs:schema targetNamespace="http://tempuri.org/DrillerData_1.0.xsd"
>> elementFormDefault="qualified"
>> xmlns="http://tempuri.org/DrillerData_1.0.xsd"
>> xmlns:xs="http://www.w3.org/2001/XMLSchema">
>>   <xs:element name="driller-data">
>>     <xs:complexType>
>>       <xs:attribute name="date" type="xs:dateTime"/>
>>       <xs:attribute name="drill-unit" type="xs:string" />
>>       <xs:attribute name="driller" type="xs:string" />
>>       <xs:attribute name="shot-point-number" type="xs:int" />
>>       <xs:attribute name="drill-depth" type="xs:float" />
>>       <xs:attribute name="drill-type">
>>         <xs:simpleType>
>>           <xs:restriction base="xs:string">
>>             <xs:enumeration value="conventional" />
>>             <xs:enumeration value="lightway" />
>>           </xs:restriction>
>>         </xs:simpleType>
>>       </xs:attribute>
>>     </xs:complexType>
>>   </xs:element>
>> </xs:schema>
>>
>> As you can see the schema allows some attributes and no child elements. 
>>
>> And to avoid misundertandings, the code: 
>>
>>     /* actual xerces work */ 
>>     char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and
>> Form
>> Templates\\DrillData_1.xml"; 
>>     char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and
>> Form
>> Templates\\newDrillData.xsd"; 
>>     SAXParser *parser = new SAXParser(); 
>>
>>     CSaxParserErrorHandler errorHandler;
>>     parser->setErrorHandler(&errorHandler);
>>
>>     parser->setDoNamespaces(true); 
>>     parser->setDoSchema(true);
>>
>>
>>     try 
>>     { 
>>        
>> parser->setExternalSchemaLocation("http://tempuri.org/DrillerData_1.0.xsd
>> D:\\Development\\Chronicle\\Example%20Schemas%20and%20Form%20Templates\\newDrillData.xsd");
>>         //parser->setExternalNoNamespaceSchemaLocation (xml_schema); 
>>         parser->parse(xml_file); 
>>         return true; 
>>     } 
>>     catch (const XMLException &e) { 
>>         char *message = XMLString::transcode(e.getMessage()); 
>>         //qDebug() << "XML Exception is: " << message << endl; 
>>         XMLString::release(&message); 
>>     } 
>>     catch(...) 
>>     { 
>>         //qDebug() << "XML Unexpected exception" << endl; 
>>     } 
>>
>> Corrupting the XML file so that the XML is not well formed e.g. remove
>> the
>> <driller-data> line, and an error is indicated. 
>>
>> Leif
>>
>>
>>
>>
>> Alberto Massari wrote:
>>   
>>> It looks you didn't read my e-mail; I said
>>>
>>> "Furthermore, in order to use schema, you should invoke
>>> setDoSchema(true)
>>> Finally, change the setExternalSchemaLocation to be 
>>> setExternalNoNamespaceSchemaLocation (or, if schema.xsd is using a 
>>> targetNamespace, change the argument of setExternalSchemaLocation to be 
>>> "uri schema.xsd") "
>>>
>>> Alberto
>>>
>>> Leif Goodwin ha scritto:
>>>     
>>>> We've tried the above code and it does not validate the XML file. All
>>>> it
>>>> does
>>>> is check that it is well formed. Here is the hacked code: 
>>>>
>>>>     /* initialize xerces system before usig API: DOM, SAX, SAX2 */ 
>>>>     try { 
>>>>         xercesc::XMLPlatformUtils::Initialize(); 
>>>>     } 
>>>>     catch (const XMLException &e) { 
>>>>         char *message = XMLString::transcode(e.getMessage()); 
>>>>         //qDebug() << "Error during XML initialization: " << message <<
>>>> endl; 
>>>>         XMLString::release(&message); 
>>>>         //return; 
>>>>     } 
>>>>
>>>>     /* actual xerces work */ 
>>>>     char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and
>>>> Form
>>>> Templates\\DrillData_1.xml"; 
>>>>     char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and
>>>> Form
>>>> Templates\\newDrillData.xsd"; 
>>>>     SAXParser *parser = new SAXParser(); 
>>>>
>>>>     CSaxParserErrorHandler errorHandler;
>>>>     parser->setErrorHandler(&errorHandler);
>>>>
>>>>     //parser->setDoValidation(true); 
>>>>     parser->setDoNamespaces(true); 
>>>>
>>>>     try 
>>>>     { 
>>>>         parser->setExternalSchemaLocation(xml_schema); 
>>>>         parser->parse(xml_file); 
>>>>         return true; 
>>>>     } 
>>>>     catch (const XMLException &e) 
>>>>     { 
>>>>         char *message = XMLString::transcode(e.getMessage()); 
>>>>         //qDebug() << "XML Exception is: " << message << endl; 
>>>>         XMLString::release(&message); 
>>>>     } 
>>>>     catch(...) 
>>>>     { 
>>>>         //qDebug() << "XML Unexpected exception" << endl; 
>>>>     } 
>>>>
>>>>     delete parser; 
>>>>     //delete doc_handler; 
>>>>
>>>>     /* terminate and cleanup */ 
>>>>     XMLPlatformUtils::Terminate(); 
>>>>
>>>> The XML file purposefully has an extra element which is not allowed by
>>>> the
>>>> schema. But the parser accepts it. 
>>>>
>>>> Here is the quickly knocked together error handler: 
>>>>
>>>> class CSaxParserErrorHandler : public xercesc_3_0::ErrorHandler
>>>> {
>>>>     virtual void warning(const SAXParseException& exc);
>>>>     virtual void error(const SAXParseException& exc);
>>>>     virtual void fatalError(const SAXParseException& exc);
>>>>     void resetErrors()
>>>>     {
>>>>     }
>>>>
>>>> };
>>>>
>>>> void CSaxParserErrorHandler::warning(const SAXParseException& exc)
>>>> {
>>>>     CReport::Warning(
>>>>         "File: %s, line %d, column %d : %s",
>>>>         StrX(exc.getSystemId()), 
>>>>         (int)exc.getLineNumber(),
>>>>         (int)exc.getLineNumber(),
>>>>         StrX(exc.getMessage()));
>>>> }
>>>>
>>>> void CSaxParserErrorHandler::error(const SAXParseException& exc)
>>>> {
>>>>     CReport::Error(
>>>>         "File: %s, line %d, column %d : %s",
>>>>         StrX(exc.getSystemId()), 
>>>>         (int)exc.getLineNumber(),
>>>>         (int)exc.getLineNumber(),
>>>>         StrX(exc.getMessage()));
>>>> }
>>>>
>>>> void CSaxParserErrorHandler::fatalError(const SAXParseException& exc)
>>>> {
>>>>     CReport::Error(
>>>>         "(Fatal Error) File: %s, line %d, column %d : %s",
>>>>         StrX(exc.getSystemId()), 
>>>>         (int)exc.getLineNumber(),
>>>>         (int)exc.getLineNumber(),
>>>>         StrX(exc.getMessage()));
>>>> }
>>>>
>>>>   
>>>>       
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
>>> For additional commands, e-mail: c-dev-help@xerces.apache.org
>>>
>>>
>>>
>>>     
>>
>>   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: c-dev-help@xerces.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/validating-xml-with-xsd-schema-tp23315036p23522096.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: validating xml with xsd schema

Posted by ashlesha1 <as...@gmail.com>.
Hi ,

I am trying to use Xerces-c to validate xml with xsd schema. I have checked
that I have done all settings mentioned in this thread. Still I am facing a
specific problem - I have an xml with its XSD schema (say XSD1) imported
inside it. I call the application with this XML and a totally different
schema (say XSD2) as an argument . Now the given XML should get validated
against XSD2 and it should return error. But it returns success. If I remove
the reference of XSD1 from the XML, then it returns failure. This shows that
somewhere it is trying to validate against that imported schema XSD1 and not
the schema provided during validation (XSD2). Can anybody please give me
some pointers on this?

Here is a snippet of my application code for your reference:

    const char *xmlFile = 0;
    char *xsdFile = 0;

    SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Auto;
    bool doNamespaces = true;
    bool doSchema = true;
    bool schemaFullChecking = false;
    bool identityConstraintChecking = true;
    bool errorOccurred = false;
    bool namespacePrefixes = true;

    // Initialize the XML4C2 system
    try
    {
        XMLPlatformUtils::Initialize();
    }
    catch(const XMLException & toCatch)
    {
        XERCES_STD_QUALIFIER cerr << "Error during initialization!
Message:\n"
            << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
        return 1;
    }

   SAX2XMLReader *parser = XMLReaderFactory::createXMLReader();

    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
    parser->setFeature(XMLUni::fgXercesSchema, doSchema);
   
parser->setFeature(XMLUni::fgXercesSchemaFullChecking,schemaFullChecking);
   
parser->setFeature(XMLUni::fgXercesIdentityConstraintChecking,identityConstraintChecking);
   
parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes,namespacePrefixes);

    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgXercesDynamic, false);
    parser->setFeature(XMLUni::fgXercesContinueAfterFatalError, false);
    parser->setFeature(XMLUni::fgXercesValidationErrorAsFatal, false);
    parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, false);

    parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,xsdFile
);
    schemaValidateHandlers handler;

    parser->setErrorHandler(&handler);
    parser->setEntityResolver(&handler);

    xmlFile = argV[1];
    xsdFile = argV[2];

    handler.resetErrors();
    try
    {
        parser->loadGrammar(xsdFile, Grammar::SchemaGrammarType, true);
        parser->parse(xmlFile);
    }
    catch(const OutOfMemoryException &)
    {       
        XERCES_STD_QUALIFIER cerr << "\nOutOfMemoryException" <<
        XERCES_STD_QUALIFIER endl;
        errorOccurred = true;
    }
       catch(const XMLException & e)
    {
        XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" <<
        xmlFile << "'\n" << "Exception message is:  \n" <<
StrX(e.getMessage()) << "\n" << XERCES_STD_QUALIFIER endl;
        errorOccurred = true;
    }
    catch (const SAXParseException& toCatch)
    {
        XERCES_STD_QUALIFIER cerr << "Error during initialization!
Message:\n"
            << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
        return -1;
    }
    catch(...)
    {
        XERCES_STD_QUALIFIER cerr << "FAILURE" << XERCES_STD_QUALIFIER
            endl;
        XERCES_STD_QUALIFIER cerr <<
        "\nUnexpected exception during parsing: '" << xmlFile <<
        "'\n";
        errorOccurred = true;
    }

    if (!handler.getSawErrors())
    {
        printf("Validation SUCCESS\n");
    } else {
        errorOccurred = true;
    }
   delete parser;
    XMLPlatformUtils::Terminate();


Regards,
-Ashlesha
  




                                                                                                                                          
91,5          28%




-- 
View this message in context: http://old.nabble.com/validating-xml-with-xsd-schema-tp23315036p28364402.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: validating xml with xsd schema

Posted by Alberto Massari <am...@datadirect.com>.
Hi Leif,
there is an extra setting missing:

    parser->setValidationScheme(SAXParser::Val_Auto); // or Val_Always, 
if you prefer

Alberto

Leif Goodwin ha scritto:
> Alberto. Apologies for missing your comment. (Easily done since they were
> embedded in long quotes.) I corrected the code and found that the
> setExternalSchemaLocation method cannot cope with folder names that contain
> whitespace. Moving the schema to a new file location, or replacing each
> space with %20, and trying the new code, it still does not work. The extra
> illegal child element in the root element is not picked up as an error.
> Incidentally we use a schema with a targetNamespace. 
>
> Test XML file: 
>
> <?xml version="1.0"?>
> <driller-data 
> 	xmlns="http://tempuri.org/DrillerData_1.0.xsd"
> 	date="8 July 2009" drill-unit="Mickey Mouse"  driller = "Joe Smith"
> shot-point-number="1" drill-depth="10.5" drill-type="conventional">
> <dummy test="4"/>
> </driller-data>
>
> Test schema file: 
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema targetNamespace="http://tempuri.org/DrillerData_1.0.xsd"
> elementFormDefault="qualified"
> xmlns="http://tempuri.org/DrillerData_1.0.xsd"
> xmlns:xs="http://www.w3.org/2001/XMLSchema">
>   <xs:element name="driller-data">
>     <xs:complexType>
>       <xs:attribute name="date" type="xs:dateTime"/>
>       <xs:attribute name="drill-unit" type="xs:string" />
>       <xs:attribute name="driller" type="xs:string" />
>       <xs:attribute name="shot-point-number" type="xs:int" />
>       <xs:attribute name="drill-depth" type="xs:float" />
>       <xs:attribute name="drill-type">
>         <xs:simpleType>
>           <xs:restriction base="xs:string">
>             <xs:enumeration value="conventional" />
>             <xs:enumeration value="lightway" />
>           </xs:restriction>
>         </xs:simpleType>
>       </xs:attribute>
>     </xs:complexType>
>   </xs:element>
> </xs:schema>
>
> As you can see the schema allows some attributes and no child elements. 
>
> And to avoid misundertandings, the code: 
>
>     /* actual xerces work */ 
>     char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and Form
> Templates\\DrillData_1.xml"; 
>     char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and Form
> Templates\\newDrillData.xsd"; 
>     SAXParser *parser = new SAXParser(); 
>
>     CSaxParserErrorHandler errorHandler;
>     parser->setErrorHandler(&errorHandler);
>
>     parser->setDoNamespaces(true); 
>     parser->setDoSchema(true);
>
>
>     try 
>     { 
>        
> parser->setExternalSchemaLocation("http://tempuri.org/DrillerData_1.0.xsd
> D:\\Development\\Chronicle\\Example%20Schemas%20and%20Form%20Templates\\newDrillData.xsd");
>         //parser->setExternalNoNamespaceSchemaLocation (xml_schema); 
>         parser->parse(xml_file); 
>         return true; 
>     } 
>     catch (const XMLException &e) { 
>         char *message = XMLString::transcode(e.getMessage()); 
>         //qDebug() << "XML Exception is: " << message << endl; 
>         XMLString::release(&message); 
>     } 
>     catch(...) 
>     { 
>         //qDebug() << "XML Unexpected exception" << endl; 
>     } 
>
> Corrupting the XML file so that the XML is not well formed e.g. remove the
> <driller-data> line, and an error is indicated. 
>
> Leif
>
>
>
>
> Alberto Massari wrote:
>   
>> It looks you didn't read my e-mail; I said
>>
>> "Furthermore, in order to use schema, you should invoke setDoSchema(true)
>> Finally, change the setExternalSchemaLocation to be 
>> setExternalNoNamespaceSchemaLocation (or, if schema.xsd is using a 
>> targetNamespace, change the argument of setExternalSchemaLocation to be 
>> "uri schema.xsd") "
>>
>> Alberto
>>
>> Leif Goodwin ha scritto:
>>     
>>> We've tried the above code and it does not validate the XML file. All it
>>> does
>>> is check that it is well formed. Here is the hacked code: 
>>>
>>>     /* initialize xerces system before usig API: DOM, SAX, SAX2 */ 
>>>     try { 
>>>         xercesc::XMLPlatformUtils::Initialize(); 
>>>     } 
>>>     catch (const XMLException &e) { 
>>>         char *message = XMLString::transcode(e.getMessage()); 
>>>         //qDebug() << "Error during XML initialization: " << message <<
>>> endl; 
>>>         XMLString::release(&message); 
>>>         //return; 
>>>     } 
>>>
>>>     /* actual xerces work */ 
>>>     char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and
>>> Form
>>> Templates\\DrillData_1.xml"; 
>>>     char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and
>>> Form
>>> Templates\\newDrillData.xsd"; 
>>>     SAXParser *parser = new SAXParser(); 
>>>
>>>     CSaxParserErrorHandler errorHandler;
>>>     parser->setErrorHandler(&errorHandler);
>>>
>>>     //parser->setDoValidation(true); 
>>>     parser->setDoNamespaces(true); 
>>>
>>>     try 
>>>     { 
>>>         parser->setExternalSchemaLocation(xml_schema); 
>>>         parser->parse(xml_file); 
>>>         return true; 
>>>     } 
>>>     catch (const XMLException &e) 
>>>     { 
>>>         char *message = XMLString::transcode(e.getMessage()); 
>>>         //qDebug() << "XML Exception is: " << message << endl; 
>>>         XMLString::release(&message); 
>>>     } 
>>>     catch(...) 
>>>     { 
>>>         //qDebug() << "XML Unexpected exception" << endl; 
>>>     } 
>>>
>>>     delete parser; 
>>>     //delete doc_handler; 
>>>
>>>     /* terminate and cleanup */ 
>>>     XMLPlatformUtils::Terminate(); 
>>>
>>> The XML file purposefully has an extra element which is not allowed by
>>> the
>>> schema. But the parser accepts it. 
>>>
>>> Here is the quickly knocked together error handler: 
>>>
>>> class CSaxParserErrorHandler : public xercesc_3_0::ErrorHandler
>>> {
>>>     virtual void warning(const SAXParseException& exc);
>>>     virtual void error(const SAXParseException& exc);
>>>     virtual void fatalError(const SAXParseException& exc);
>>>     void resetErrors()
>>>     {
>>>     }
>>>
>>> };
>>>
>>> void CSaxParserErrorHandler::warning(const SAXParseException& exc)
>>> {
>>>     CReport::Warning(
>>>         "File: %s, line %d, column %d : %s",
>>>         StrX(exc.getSystemId()), 
>>>         (int)exc.getLineNumber(),
>>>         (int)exc.getLineNumber(),
>>>         StrX(exc.getMessage()));
>>> }
>>>
>>> void CSaxParserErrorHandler::error(const SAXParseException& exc)
>>> {
>>>     CReport::Error(
>>>         "File: %s, line %d, column %d : %s",
>>>         StrX(exc.getSystemId()), 
>>>         (int)exc.getLineNumber(),
>>>         (int)exc.getLineNumber(),
>>>         StrX(exc.getMessage()));
>>> }
>>>
>>> void CSaxParserErrorHandler::fatalError(const SAXParseException& exc)
>>> {
>>>     CReport::Error(
>>>         "(Fatal Error) File: %s, line %d, column %d : %s",
>>>         StrX(exc.getSystemId()), 
>>>         (int)exc.getLineNumber(),
>>>         (int)exc.getLineNumber(),
>>>         StrX(exc.getMessage()));
>>> }
>>>
>>>   
>>>       
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
>> For additional commands, e-mail: c-dev-help@xerces.apache.org
>>
>>
>>
>>     
>
>   


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


Re: validating xml with xsd schema

Posted by Leif Goodwin <lg...@qgsl.com>.
Alberto. Apologies for missing your comment. (Easily done since they were
embedded in long quotes.) I corrected the code and found that the
setExternalSchemaLocation method cannot cope with folder names that contain
whitespace. Moving the schema to a new file location, or replacing each
space with %20, and trying the new code, it still does not work. The extra
illegal child element in the root element is not picked up as an error.
Incidentally we use a schema with a targetNamespace. 

Test XML file: 

<?xml version="1.0"?>
<driller-data 
	xmlns="http://tempuri.org/DrillerData_1.0.xsd"
	date="8 July 2009" drill-unit="Mickey Mouse"  driller = "Joe Smith"
shot-point-number="1" drill-depth="10.5" drill-type="conventional">
<dummy test="4"/>
</driller-data>

Test schema file: 

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://tempuri.org/DrillerData_1.0.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/DrillerData_1.0.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="driller-data">
    <xs:complexType>
      <xs:attribute name="date" type="xs:dateTime"/>
      <xs:attribute name="drill-unit" type="xs:string" />
      <xs:attribute name="driller" type="xs:string" />
      <xs:attribute name="shot-point-number" type="xs:int" />
      <xs:attribute name="drill-depth" type="xs:float" />
      <xs:attribute name="drill-type">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="conventional" />
            <xs:enumeration value="lightway" />
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>

As you can see the schema allows some attributes and no child elements. 

And to avoid misundertandings, the code: 

    /* actual xerces work */ 
    char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and Form
Templates\\DrillData_1.xml"; 
    char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and Form
Templates\\newDrillData.xsd"; 
    SAXParser *parser = new SAXParser(); 

    CSaxParserErrorHandler errorHandler;
    parser->setErrorHandler(&errorHandler);

    parser->setDoNamespaces(true); 
    parser->setDoSchema(true);


    try 
    { 
       
parser->setExternalSchemaLocation("http://tempuri.org/DrillerData_1.0.xsd
D:\\Development\\Chronicle\\Example%20Schemas%20and%20Form%20Templates\\newDrillData.xsd");
        //parser->setExternalNoNamespaceSchemaLocation (xml_schema); 
        parser->parse(xml_file); 
        return true; 
    } 
    catch (const XMLException &e) { 
        char *message = XMLString::transcode(e.getMessage()); 
        //qDebug() << "XML Exception is: " << message << endl; 
        XMLString::release(&message); 
    } 
    catch(...) 
    { 
        //qDebug() << "XML Unexpected exception" << endl; 
    } 

Corrupting the XML file so that the XML is not well formed e.g. remove the
<driller-data> line, and an error is indicated. 

Leif




Alberto Massari wrote:
> 
> It looks you didn't read my e-mail; I said
> 
> "Furthermore, in order to use schema, you should invoke setDoSchema(true)
> Finally, change the setExternalSchemaLocation to be 
> setExternalNoNamespaceSchemaLocation (or, if schema.xsd is using a 
> targetNamespace, change the argument of setExternalSchemaLocation to be 
> "uri schema.xsd") "
> 
> Alberto
> 
> Leif Goodwin ha scritto:
>> We've tried the above code and it does not validate the XML file. All it
>> does
>> is check that it is well formed. Here is the hacked code: 
>>
>>     /* initialize xerces system before usig API: DOM, SAX, SAX2 */ 
>>     try { 
>>         xercesc::XMLPlatformUtils::Initialize(); 
>>     } 
>>     catch (const XMLException &e) { 
>>         char *message = XMLString::transcode(e.getMessage()); 
>>         //qDebug() << "Error during XML initialization: " << message <<
>> endl; 
>>         XMLString::release(&message); 
>>         //return; 
>>     } 
>>
>>     /* actual xerces work */ 
>>     char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and
>> Form
>> Templates\\DrillData_1.xml"; 
>>     char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and
>> Form
>> Templates\\newDrillData.xsd"; 
>>     SAXParser *parser = new SAXParser(); 
>>
>>     CSaxParserErrorHandler errorHandler;
>>     parser->setErrorHandler(&errorHandler);
>>
>>     //parser->setDoValidation(true); 
>>     parser->setDoNamespaces(true); 
>>
>>     try 
>>     { 
>>         parser->setExternalSchemaLocation(xml_schema); 
>>         parser->parse(xml_file); 
>>         return true; 
>>     } 
>>     catch (const XMLException &e) 
>>     { 
>>         char *message = XMLString::transcode(e.getMessage()); 
>>         //qDebug() << "XML Exception is: " << message << endl; 
>>         XMLString::release(&message); 
>>     } 
>>     catch(...) 
>>     { 
>>         //qDebug() << "XML Unexpected exception" << endl; 
>>     } 
>>
>>     delete parser; 
>>     //delete doc_handler; 
>>
>>     /* terminate and cleanup */ 
>>     XMLPlatformUtils::Terminate(); 
>>
>> The XML file purposefully has an extra element which is not allowed by
>> the
>> schema. But the parser accepts it. 
>>
>> Here is the quickly knocked together error handler: 
>>
>> class CSaxParserErrorHandler : public xercesc_3_0::ErrorHandler
>> {
>>     virtual void warning(const SAXParseException& exc);
>>     virtual void error(const SAXParseException& exc);
>>     virtual void fatalError(const SAXParseException& exc);
>>     void resetErrors()
>>     {
>>     }
>>
>> };
>>
>> void CSaxParserErrorHandler::warning(const SAXParseException& exc)
>> {
>>     CReport::Warning(
>>         "File: %s, line %d, column %d : %s",
>>         StrX(exc.getSystemId()), 
>>         (int)exc.getLineNumber(),
>>         (int)exc.getLineNumber(),
>>         StrX(exc.getMessage()));
>> }
>>
>> void CSaxParserErrorHandler::error(const SAXParseException& exc)
>> {
>>     CReport::Error(
>>         "File: %s, line %d, column %d : %s",
>>         StrX(exc.getSystemId()), 
>>         (int)exc.getLineNumber(),
>>         (int)exc.getLineNumber(),
>>         StrX(exc.getMessage()));
>> }
>>
>> void CSaxParserErrorHandler::fatalError(const SAXParseException& exc)
>> {
>>     CReport::Error(
>>         "(Fatal Error) File: %s, line %d, column %d : %s",
>>         StrX(exc.getSystemId()), 
>>         (int)exc.getLineNumber(),
>>         (int)exc.getLineNumber(),
>>         StrX(exc.getMessage()));
>> }
>>
>>   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: c-dev-help@xerces.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/validating-xml-with-xsd-schema-tp23315036p23516830.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: validating xml with xsd schema

Posted by Alberto Massari <am...@datadirect.com>.
It looks you didn't read my e-mail; I said

"Furthermore, in order to use schema, you should invoke setDoSchema(true)
Finally, change the setExternalSchemaLocation to be 
setExternalNoNamespaceSchemaLocation (or, if schema.xsd is using a 
targetNamespace, change the argument of setExternalSchemaLocation to be 
"uri schema.xsd") "

Alberto

Leif Goodwin ha scritto:
> We've tried the above code and it does not validate the XML file. All it does
> is check that it is well formed. Here is the hacked code: 
>
>     /* initialize xerces system before usig API: DOM, SAX, SAX2 */ 
>     try { 
>         xercesc::XMLPlatformUtils::Initialize(); 
>     } 
>     catch (const XMLException &e) { 
>         char *message = XMLString::transcode(e.getMessage()); 
>         //qDebug() << "Error during XML initialization: " << message <<
> endl; 
>         XMLString::release(&message); 
>         //return; 
>     } 
>
>     /* actual xerces work */ 
>     char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and Form
> Templates\\DrillData_1.xml"; 
>     char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and Form
> Templates\\newDrillData.xsd"; 
>     SAXParser *parser = new SAXParser(); 
>
>     CSaxParserErrorHandler errorHandler;
>     parser->setErrorHandler(&errorHandler);
>
>     //parser->setDoValidation(true); 
>     parser->setDoNamespaces(true); 
>
>     try 
>     { 
>         parser->setExternalSchemaLocation(xml_schema); 
>         parser->parse(xml_file); 
>         return true; 
>     } 
>     catch (const XMLException &e) 
>     { 
>         char *message = XMLString::transcode(e.getMessage()); 
>         //qDebug() << "XML Exception is: " << message << endl; 
>         XMLString::release(&message); 
>     } 
>     catch(...) 
>     { 
>         //qDebug() << "XML Unexpected exception" << endl; 
>     } 
>
>     delete parser; 
>     //delete doc_handler; 
>
>     /* terminate and cleanup */ 
>     XMLPlatformUtils::Terminate(); 
>
> The XML file purposefully has an extra element which is not allowed by the
> schema. But the parser accepts it. 
>
> Here is the quickly knocked together error handler: 
>
> class CSaxParserErrorHandler : public xercesc_3_0::ErrorHandler
> {
>     virtual void warning(const SAXParseException& exc);
>     virtual void error(const SAXParseException& exc);
>     virtual void fatalError(const SAXParseException& exc);
>     void resetErrors()
>     {
>     }
>
> };
>
> void CSaxParserErrorHandler::warning(const SAXParseException& exc)
> {
>     CReport::Warning(
>         "File: %s, line %d, column %d : %s",
>         StrX(exc.getSystemId()), 
>         (int)exc.getLineNumber(),
>         (int)exc.getLineNumber(),
>         StrX(exc.getMessage()));
> }
>
> void CSaxParserErrorHandler::error(const SAXParseException& exc)
> {
>     CReport::Error(
>         "File: %s, line %d, column %d : %s",
>         StrX(exc.getSystemId()), 
>         (int)exc.getLineNumber(),
>         (int)exc.getLineNumber(),
>         StrX(exc.getMessage()));
> }
>
> void CSaxParserErrorHandler::fatalError(const SAXParseException& exc)
> {
>     CReport::Error(
>         "(Fatal Error) File: %s, line %d, column %d : %s",
>         StrX(exc.getSystemId()), 
>         (int)exc.getLineNumber(),
>         (int)exc.getLineNumber(),
>         StrX(exc.getMessage()));
> }
>
>   


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


Re: validating xml with xsd schema

Posted by Leif Goodwin <lg...@qgsl.com>.
We've tried the above code and it does not validate the XML file. All it does
is check that it is well formed. Here is the hacked code: 

    /* initialize xerces system before usig API: DOM, SAX, SAX2 */ 
    try { 
        xercesc::XMLPlatformUtils::Initialize(); 
    } 
    catch (const XMLException &e) { 
        char *message = XMLString::transcode(e.getMessage()); 
        //qDebug() << "Error during XML initialization: " << message <<
endl; 
        XMLString::release(&message); 
        //return; 
    } 

    /* actual xerces work */ 
    char *xml_file = "D:\\Development\\Chronicle\\Example Schemas and Form
Templates\\DrillData_1.xml"; 
    char *xml_schema = "D:\\Development\\Chronicle\\Example Schemas and Form
Templates\\newDrillData.xsd"; 
    SAXParser *parser = new SAXParser(); 

    CSaxParserErrorHandler errorHandler;
    parser->setErrorHandler(&errorHandler);

    //parser->setDoValidation(true); 
    parser->setDoNamespaces(true); 

    try 
    { 
        parser->setExternalSchemaLocation(xml_schema); 
        parser->parse(xml_file); 
        return true; 
    } 
    catch (const XMLException &e) 
    { 
        char *message = XMLString::transcode(e.getMessage()); 
        //qDebug() << "XML Exception is: " << message << endl; 
        XMLString::release(&message); 
    } 
    catch(...) 
    { 
        //qDebug() << "XML Unexpected exception" << endl; 
    } 

    delete parser; 
    //delete doc_handler; 

    /* terminate and cleanup */ 
    XMLPlatformUtils::Terminate(); 

The XML file purposefully has an extra element which is not allowed by the
schema. But the parser accepts it. 

Here is the quickly knocked together error handler: 

class CSaxParserErrorHandler : public xercesc_3_0::ErrorHandler
{
    virtual void warning(const SAXParseException& exc);
    virtual void error(const SAXParseException& exc);
    virtual void fatalError(const SAXParseException& exc);
    void resetErrors()
    {
    }

};

void CSaxParserErrorHandler::warning(const SAXParseException& exc)
{
    CReport::Warning(
        "File: %s, line %d, column %d : %s",
        StrX(exc.getSystemId()), 
        (int)exc.getLineNumber(),
        (int)exc.getLineNumber(),
        StrX(exc.getMessage()));
}

void CSaxParserErrorHandler::error(const SAXParseException& exc)
{
    CReport::Error(
        "File: %s, line %d, column %d : %s",
        StrX(exc.getSystemId()), 
        (int)exc.getLineNumber(),
        (int)exc.getLineNumber(),
        StrX(exc.getMessage()));
}

void CSaxParserErrorHandler::fatalError(const SAXParseException& exc)
{
    CReport::Error(
        "(Fatal Error) File: %s, line %d, column %d : %s",
        StrX(exc.getSystemId()), 
        (int)exc.getLineNumber(),
        (int)exc.getLineNumber(),
        StrX(exc.getMessage()));
}

-- 
View this message in context: http://www.nabble.com/validating-xml-with-xsd-schema-tp23315036p23505306.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: validating xml with xsd schema

Posted by David Bertoni <db...@apache.org>.
evangeline wrote:
> So, I should declare a class that looks like this:
> 
> class XMLValidateError : public HandlerBase {
>   public:
>     XMLValidateError();
>     void error(XMLException &e);
This signature is wrong, so you aren't overriding the base class 
function.  The correct signature is:

virtual void error(const SAXParseException& exc);

> XMLValidateError::XMLValidateError() {
> 
> }
> 
> void XMLValidateError::error(XMLException &e) {
>         char *message = XMLString::transcode(e.getMessage());
Please make sure you call XMLString::release(&message), or this will be 
a memory leak.

>         qDebug() << "XML Error is: " << message << endl;
> }
> 
> and then:
> XMLValidateError *err_handler = new XMLValidateError();
> 
> But I still don't get any error messages and it still always returns
> true...any thoughts ?
Correct the signature and you will get the error event.

Dave

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