[xsd-users] How to validate XML using a in-memory schema

Boris Kolpackov boris at codesynthesis.com
Thu Sep 28 07:54:22 EDT 2006


Hi Raul,

Raul Huertas <raulh39 at tid.es> writes:

> I don't feel comfortable with this way of doing things because there is
> no runtime way to verify that the file "/absolute/path/to/schema.xsd" is
> the same file that was used to generate C++ code with the xsd tool, is it?

No there is no automatic way do do this. Of course you could always compute
a hash sum of the schema file and compare it with the one embedded into the
application to detect the schema/generated code mismatch. This will require
some manual coding, however.


> Can I have the schema loaded in memory, in a std::string for example,
> and then use this string to validate XML?
> Like:
>    std::string myXsd ="<xs:schema ....>..."
>    props.no_namespace_schema_location.use (myXsd);

There is no built-in way but you can achieve this if you are willing
to perform the XML-to-DOM stage yourself. At the same time you can also
cache the schema so that if you parse multiple instance documents the
schema is read and parsed only once.

First check the "How do I parse an XML instance to a Xerces-C++ DOM
document?" question on the C++/Tree Wiki FAQ[1]; it has the code that
does the XML to DOM parsing. You will need to add the following
includes at the beginning:


#include <xercesc/framework/MemBufInputSource.hpp>
#include <xsd/cxx/xml/string.hxx>


And the following code after the "Enable/Disable validation" lines:


std::string my_xsd ="<xs:schema ....>...";
MemBufInputSource mis (my_xsd.c_str (), my_xsd.size (), "/schema.xsd");
Wrapper4InputSource wmis (mis, false);

parser->loadGrammar (wmis, Grammar::SchemaGrammarType, true);
parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);

parser->setProperty (
  XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
  const_cast<void*> (xml::string ("file:///schema.xsd").c_str ()));


Note that the "/schema.xsd" file does not have to exist since it will
be resolved from the cache. I haven't actually tested this code so let
me know whether it works or not ;-).

Once you obtain the DOM document simply invoke the generated parsing
function as you would do on a file name or an std::istream instance.

There is also a nice article[2] on IBM DevWorks if you want more
information on schema caching in Xerces-C++.

> Or, better than that, can the xsd tool save the schema in the generated
> C++ code and then use it to validate XML?

We actually thought about this feature. The problem is that it does not
scale to the case where you have included/imported schemas.


[1] http://wiki.codesynthesis.com/Tree/FAQ
[2] http://www-128.ibm.com/developerworks/webservices/library/x-xsdxerc.html


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060928/d641b19b/attachment.pgp


More information about the xsd-users mailing list