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: [email protected]
For additional commands, e-mail: [email protected]






---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to