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 Mathieu Champlon <ma...@masagroup.net> on 2010/11/18 12:52:12 UTC

MemBufInputSource and xs:include

Hello,

I have an XML file validated by xerces against a schema_1.xsd file which 
xs:include's another schema_2.xsd file.
So far so good, by now I would like to move those files into memory.
I have been using a MemBufInputSource before for this, but there wasn't 
any xs:include then.
Of course I cannot alter the schemas in any way. :)

Here is what I have been trying (xerces 3.x version) :

using namespace XERCES_CPP_NAMESPACE;

DOMLSParser& parser = ...
parser.getDomConfig()->setParameter( 
XMLUni::fgXercesUserAdoptsDOMDocument, true );
parser.getDomConfig()->setParameter( XMLUni::fgDOMNamespaces, true );
parser.getDomConfig()->setParameter( XMLUni::fgDOMDatatypeNormalization, 
true );
parser.getDomConfig()->setParameter( XMLUni::fgXercesSchema, true );
parser.getDomConfig()->setParameter( XMLUni::fgDOMValidate, true );
parser.getDomConfig()->setParameter( 
XMLUni::fgXercesUseCachedGrammarInParse, true  );
const std::string schema_1 = "<xs:schema 
xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
                              " <xs:include schemaLocation='schema_2.xsd'/>"
                              " <xs:element name='element'>"
                              " <xs:complexType>"
                              " <xs:attribute name='attribute' 
type='type'/>"
                              " </xs:complexType>"
                              " </xs:element>"
                              "</xs:schema>";
const std::string schema_2 = "<xs:schema 
xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
                              " <xs:simpleType name='type'>"
                              " <xs:restriction base='xs:int'/>"
                              " </xs:simpleType>"
                              "</xs:schema>";
{
   MemBufInputSource source( reinterpret_cast< const XMLByte* >( 
schema_1.c_str() ), schema_1.size(), "schema_1.xsd" );
   Wrapper4InputSource input( &source, false );
   if( ! parser.loadGrammar( &input, Grammar::SchemaGrammarType, true ) )
     throw ...
}
{
   MemBufInputSource source( reinterpret_cast< const XMLByte* >( 
schema_2.c_str() ), schema_2.size(), "schema_2.xsd" );
   Wrapper4InputSource input( &source, false );
   if( ! parser.loadGrammar( &input, Grammar::SchemaGrammarType, true ) )
     throw ...
}

And then parsing the following XML document : <element attribute='42'/>
Yields the validation error : unable to find validator for simple type 
of attribute 'attribute'

The validation succeeds however if the content of schema_2 is moved to a 
schema_2.xsd file in the working directory.

Is what I am looking for theoretically possible ?
If yes I would really appreciate being pointed to the right direction. :)

Thanks a lot for any hint !

Regards,
MAT.


Re: MemBufInputSource and xs:include

Posted by Mathieu Champlon <ma...@masagroup.net>.
On 18/11/2010 17:33, Mathieu Champlon wrote:
> On 18/11/2010 13:52, Alberto Massari wrote:
>> Uhm, maybe it's because both schemas use the empty namespace, so the 
>> second one is not loaded at all.
>> You could try setting the parameter 
>> XMLUni::fgXercesHandleMultipleImports to true; failing that, you will 
>> have to switch using the resource resolver.
>>
>
> fgXercesHandleMultipleImports doesn't seem to make any difference, nor 
> adding a namespace, neither adding a different namespace for each 
> schema for that matter...

Hmm ok, I had actually got mixed up in my test cases.
It seems that fgXercesHandleMultipleImports does indeed enable the 
behaviour I am looking for.

Thanks again Alberto !

MAT.


Re: MemBufInputSource and xs:include

Posted by Mathieu Champlon <ma...@masagroup.net>.
On 18/11/2010 13:52, Alberto Massari wrote:
> Uhm, maybe it's because both schemas use the empty namespace, so the 
> second one is not loaded at all.
> You could try setting the parameter 
> XMLUni::fgXercesHandleMultipleImports to true; failing that, you will 
> have to switch using the resource resolver.
>

fgXercesHandleMultipleImports doesn't seem to make any difference, nor 
adding a namespace, neither adding a different namespace for each schema 
for that matter...
I'll look into registering a custom resolver then.

Thank you,
MAT.


Re: MemBufInputSource and xs:include

Posted by Alberto Massari <Al...@progress.com>.
Uhm, maybe it's because both schemas use the empty namespace, so the 
second one is not loaded at all.
You could try setting the parameter 
XMLUni::fgXercesHandleMultipleImports to true; failing that, you will 
have to switch using the resource resolver.

Alberto

On 11/18/2010 1:32 PM, Mathieu Champlon wrote:
> On 18/11/2010 12:03, Alberto Massari wrote:
>> Hi Mathieu,
>> the proper way of handling this kind of configuration is by
>> registering a resource resolver that, when asked for "schema2.xsd",
>> returns the correct MemBufInputSource.
>> Otherwise you could fix your code by parsing first the schema_2 and
>> then the schema_1 buffer, so that schema_1 can find schema_2 already
>> loaded in the grammar cache.
>>
> Thank you for your help Alberto,
>
> When parsing first schema_2 and then schema_1 I get this error upon
> validation : no declaration found for element 'element'
> Therefore I was under the impression that the first parsed schema was
> supposed to contain the definition for the root element ?
>
> I'll look into registering a custom resource resolver, thanks for
> pointing that out.
>
> MAT.
>
>


RE: MemBufInputSource and xs:include

Posted by John Lilley <jl...@datalever.com>.
To remove your address from the list, send a message to:
   <c-...@xerces.apache.org>

-----Original Message-----
From: Josep Oriol Ragués [mailto:j.o.ragues@gmail.com] 
Sent: Thursday, November 18, 2010 5:43 AM
To: c-users@xerces.apache.org
Subject: Re: MemBufInputSource and xs:include

Hi, I need to unsubscribe, don't know how !

Thanks

2010/11/18 Mathieu Champlon <ma...@masagroup.net>

> On 18/11/2010 12:03, Alberto Massari wrote:
>
>> Hi Mathieu,
>> the proper way of handling this kind of configuration is by registering a
>> resource resolver that, when asked for "schema2.xsd", returns the correct
>> MemBufInputSource.
>> Otherwise you could fix your code by parsing first the schema_2 and then
>> the schema_1 buffer, so that schema_1 can find schema_2 already loaded in
>> the grammar cache.
>>
>>
> Thank you for your help Alberto,
>
> When parsing first schema_2 and then schema_1 I get this error upon
> validation : no declaration found for element 'element'
> Therefore I was under the impression that the first parsed schema was
> supposed to contain the definition for the root element ?
>
> I'll look into registering a custom resource resolver, thanks for pointing
> that out.
>
> MAT.
>
>


-- 
- En nombre del código abierto, vegano y libre de drogas -

Re: MemBufInputSource and xs:include

Posted by Josep Oriol Ragués <j....@gmail.com>.
Hi, I need to unsubscribe, don't know how !

Thanks

2010/11/18 Mathieu Champlon <ma...@masagroup.net>

> On 18/11/2010 12:03, Alberto Massari wrote:
>
>> Hi Mathieu,
>> the proper way of handling this kind of configuration is by registering a
>> resource resolver that, when asked for "schema2.xsd", returns the correct
>> MemBufInputSource.
>> Otherwise you could fix your code by parsing first the schema_2 and then
>> the schema_1 buffer, so that schema_1 can find schema_2 already loaded in
>> the grammar cache.
>>
>>
> Thank you for your help Alberto,
>
> When parsing first schema_2 and then schema_1 I get this error upon
> validation : no declaration found for element 'element'
> Therefore I was under the impression that the first parsed schema was
> supposed to contain the definition for the root element ?
>
> I'll look into registering a custom resource resolver, thanks for pointing
> that out.
>
> MAT.
>
>


-- 
- En nombre del código abierto, vegano y libre de drogas -

Re: MemBufInputSource and xs:include

Posted by Mathieu Champlon <ma...@masagroup.net>.
On 18/11/2010 12:03, Alberto Massari wrote:
> Hi Mathieu,
> the proper way of handling this kind of configuration is by 
> registering a resource resolver that, when asked for "schema2.xsd", 
> returns the correct MemBufInputSource.
> Otherwise you could fix your code by parsing first the schema_2 and 
> then the schema_1 buffer, so that schema_1 can find schema_2 already 
> loaded in the grammar cache.
>

Thank you for your help Alberto,

When parsing first schema_2 and then schema_1 I get this error upon 
validation : no declaration found for element 'element'
Therefore I was under the impression that the first parsed schema was 
supposed to contain the definition for the root element ?

I'll look into registering a custom resource resolver, thanks for 
pointing that out.

MAT.


Re: MemBufInputSource and xs:include

Posted by Alberto Massari <Al...@progress.com>.
Hi Mathieu,
the proper way of handling this kind of configuration is by registering 
a resource resolver that, when asked for "schema2.xsd", returns the 
correct MemBufInputSource.
Otherwise you could fix your code by parsing first the schema_2 and then 
the schema_1 buffer, so that schema_1 can find schema_2 already loaded 
in the grammar cache.

Alberto

On 11/18/2010 12:52 PM, Mathieu Champlon wrote:
> Hello,
>
> I have an XML file validated by xerces against a schema_1.xsd file which
> xs:include's another schema_2.xsd file.
> So far so good, by now I would like to move those files into memory.
> I have been using a MemBufInputSource before for this, but there wasn't
> any xs:include then.
> Of course I cannot alter the schemas in any way. :)
>
> Here is what I have been trying (xerces 3.x version) :
>
> using namespace XERCES_CPP_NAMESPACE;
>
> DOMLSParser&  parser = ...
> parser.getDomConfig()->setParameter(
> XMLUni::fgXercesUserAdoptsDOMDocument, true );
> parser.getDomConfig()->setParameter( XMLUni::fgDOMNamespaces, true );
> parser.getDomConfig()->setParameter( XMLUni::fgDOMDatatypeNormalization,
> true );
> parser.getDomConfig()->setParameter( XMLUni::fgXercesSchema, true );
> parser.getDomConfig()->setParameter( XMLUni::fgDOMValidate, true );
> parser.getDomConfig()->setParameter(
> XMLUni::fgXercesUseCachedGrammarInParse, true  );
> const std::string schema_1 = "<xs:schema
> xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
>                                "<xs:include schemaLocation='schema_2.xsd'/>"
>                                "<xs:element name='element'>"
>                                "<xs:complexType>"
>                                "<xs:attribute name='attribute'
> type='type'/>"
>                                "</xs:complexType>"
>                                "</xs:element>"
>                                "</xs:schema>";
> const std::string schema_2 = "<xs:schema
> xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
>                                "<xs:simpleType name='type'>"
>                                "<xs:restriction base='xs:int'/>"
>                                "</xs:simpleType>"
>                                "</xs:schema>";
> {
>     MemBufInputSource source( reinterpret_cast<  const XMLByte*>(
> schema_1.c_str() ), schema_1.size(), "schema_1.xsd" );
>     Wrapper4InputSource input(&source, false );
>     if( ! parser.loadGrammar(&input, Grammar::SchemaGrammarType, true ) )
>       throw ...
> }
> {
>     MemBufInputSource source( reinterpret_cast<  const XMLByte*>(
> schema_2.c_str() ), schema_2.size(), "schema_2.xsd" );
>     Wrapper4InputSource input(&source, false );
>     if( ! parser.loadGrammar(&input, Grammar::SchemaGrammarType, true ) )
>       throw ...
> }
>
> And then parsing the following XML document :<element attribute='42'/>
> Yields the validation error : unable to find validator for simple type
> of attribute 'attribute'
>
> The validation succeeds however if the content of schema_2 is moved to a
> schema_2.xsd file in the working directory.
>
> Is what I am looking for theoretically possible ?
> If yes I would really appreciate being pointed to the right direction. :)
>
> Thanks a lot for any hint !
>
> Regards,
> MAT.
>
> .
>