You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Bill Moo <cp...@gmail.com> on 2013/04/06 16:52:39 UTC

Validating XML using in memory Schema fails.

Hello List,

I am using xerces-c 3.1.1, Visual Studio 2012 and I am trying to validate
an XML document using a Schema I extract from a binary resource (DLL).
However, this fails unless the schema referenced in the XML is present on
disk, even if I feed the Schema via an InputSource, because if I rename the
one on disk the messages generated implies that it has no schema, either in
memory or on disk.

This is the code I have :

HMODULE hModule = ::LoadLibraryEx("Resource.dll", NULL,
LOAD_LIBRARY_AS_DATAFILE) ; if (hModule)
{
    HRSRC schemaResource = ::FindResource(hModule,
MAKEINTRESOURCE(ID_CONFIG), MAKEINTRESOURCE(RT_SCHEMA)) ;
if
(schemaResource != NULL)
{
        HGLOBAL resource = ::LoadResource(hModule, schemaResource) ;
        const XMLByte * lpResource = (const XMLByte *)
        LockResource(resource) ; DWORD dwResSize =
::SizeofResource(hModule, schemaResource) ;
        MemBufInputSource * source = new MemBufInputSource(lpResource,
(XMLSize_t) dwResSize, "InMem", false) ;
        XercesDOMParser domParser ;
       if (domParser.loadGrammar(*source,
Grammar::GrammarType::SchemaGrammarType, false) != NULL)
      {
            ParserErrorHandler * errorHandler = new
            ParserErrorHandler() ; domParser.setErrorHandler(errorHandler) ;
            domParser.setExitOnFirstFatalError(false) ;

domParser.setValidationScheme(XercesDOMParser::ValSchemes::Val_Always) ;
            domParser.setDoNamespaces(true) ;
            domParser.setDoSchema(true) ;
            domParser.setValidationConstraintFatal(true) ;
            domParser.parse(configFile) ;
            if (errorHandler) delete errorHandler ;
            success = (domParser.getErrorCount() == 0 ? true : false) ;
        }
        else
        {
            std::cout << "Failed to load Schema" << std::endl ;
        }
        if (source) delete source ;
        if (resource) ::FreeResource(resource) ;
    }
    else
    {
        std::cout << "Unable to find Schema in Resource " << std::endl ;
    }
    if (schemaResource) ::FreeResource(schemaResource) ;
}
else
{
    std::cout << "Unable to load resources DLL!" << std::endl ;
}
if (hModule) ::FreeLibrary(hModule) ;

As you can see I am under the impression that I am providing the domParser
with the schema via the MemBufInputSource but this appears to be getting
ignored,  as the Schema must be on the disk in order to get a correctly
validated document.

Ideally I want to load an XML file that does not contain (need) an
xsi:noNamespaceSchemaLocation attribute and validate it using the schema
extracted from my DLL.

Partial XML File

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Configuration.xsd" >
<!-- XML Content -->
</configuration>

Partial Schema File

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="configuration">
  <!-- Rest of schema -->
  </xs:element>
</xs:schema>

Any help on this would be really appreciated, as I suspect I may be missing
something so obvious I can't see it.


-- 
Bill