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 Simon Kitching <si...@ecnetwork.co.nz> on 2004/07/14 06:34:58 UTC

validating root element with no namespace against schema

Hi All,

I'm writing some code to validate a series of documents against a
schema.

These documents have a root node with no namespace, and no schema
declaration within the document, eg

 <root>
   <stuff/>
 </root>

I preload the required schema into the SAX2XMLReader class using
"loadGrammar". It works fine when the schema has a targetNamespace and
the root element of the input xml has a namespace, eg I can process this
fine:
 <base:root xmlns:root="zzzz">
   <stuff/>
 </base:root>
with a (preloaded) schema whose targetNamespace="zzzz".

If I preload a schema with no namespace into a SAX2XMLReader instance, I
can successfully retrieve it with
  parser.getGrammar(XMLString::transcode(""))
so it appears loaded ok.

But then parsing xml like:
 <root>
   <stuff/>
 </root>
simply reports "unknown element 'root'".

The noNamespaceSchemaLocation property doesn't seem to do what I want;
that allows me to specify the SystemId value of a schema - but the
schema is already loaded into the parser.

Any ideas how I can get the parser to use the preloaded schema with no
target namespace to validate a root element with no declared namespace?

Thanks,

Simon

PS: Sorry for posting to the dev list. I can't see a user list
anywhere...


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


Re: validating root element with no namespace against schema

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Wed, 2004-07-14 at 16:34, Simon Kitching wrote:
> Hi All,
> 
> I'm writing some code to validate a series of documents against a
> schema.
> 
> These documents have a root node with no namespace, and no schema
> declaration within the document, eg
> 
>  <root>
>    <stuff/>
>  </root>
> 
> I preload the required schema into the SAX2XMLReader class using
> "loadGrammar". It works fine when the schema has a targetNamespace and
> the root element of the input xml has a namespace, eg I can process this
> fine:
>  <base:root xmlns:root="zzzz">
>    <stuff/>
>  </base:root>
> with a (preloaded) schema whose targetNamespace="zzzz".
> 
> If I preload a schema with no namespace into a SAX2XMLReader instance, I
> can successfully retrieve it with
>   parser.getGrammar(XMLString::transcode(""))
> so it appears loaded ok.
> 
> But then parsing xml like:
>  <root>
>    <stuff/>
>  </root>
> simply reports "unknown element 'root'".
> 
> The noNamespaceSchemaLocation property doesn't seem to do what I want;
> that allows me to specify the SystemId value of a schema - but the
> schema is already loaded into the parser.
> 
> Any ideas how I can get the parser to use the preloaded schema with no
> target namespace to validate a root element with no declared namespace?
> 
> Thanks,
> 
> Simon
> 
> PS: Sorry for posting to the dev list. I can't see a user list
> anywhere...
> 

Ok, I've resolved the issue. Even though I've got the schema preloaded,
I still need to set the external-noNamespaceSchema property with some
random systemId value in order to get the parser to look in the cached
schemas.

The cause is this code in IGXMLScanner.cpp (or maybe SGXMLScanner.cpp;
I'm not sure what the IG and SG prefixes indicate):
    if (isRoot
        && fDoSchema
        && (fExternalSchemaLocation ||
       fExternalNoNamespaceSchemaLocation)) {

        if (fExternalSchemaLocation)
            parseSchemaLocation(fExternalSchemaLocation);
        if (fExternalNoNamespaceSchemaLocation)
            resolveSchemaGrammar(fExternalNoNamespaceSchemaLocation,
       XMLUni::fgZeroLenString);
    }


The if  clauses prevent any attempt to resolve the schema grammar unless
fExternalNoNamespaceSchemaLocation is set, despite the fact that this
value is not needed if the needed grammar is already cached.

But setting the "external-noNamespaceSchema" property to a dummy value
solves the problem for me, so I'm happy.

One other odd item struck me: the SAX2XMLReader::setProperty method
takes a non-const parameter as the property value, meaning that a
literal string cannot be passed in here. I don't imagine the setProperty
method ever modifies its value parameter, right? So shouldn't this value
parameter be declared const, allowing literals to be passed?

Anyway, thanks for some great software....

Regards,

Simon


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