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 "EXT-Pennington, Dale K" <da...@boeing.com> on 2007/09/14 19:41:43 UTC
More than one Schema Possible ?
For background info, I am an experienced C/C++ developer, but rather new
to XML.
I have downloaded Xerces, build the samples, and have the basics down.
The issue I am looking at is as follows :
We are tieing into an 3rd party interface, that send XML formated
messages over UDP packets. There is more than one message format
possible, each with its own specified XML Schema file. So when I get a
packet of text in, I do not automatically know which message format it
is (i.e. which Schema to use to parse and validate it). Also, they say
using a mininal prologue, which I do not expect to contain a Schema
reference (and even if it did, this is a closed set of systems and we
will each have our own copies of the Schema most like stored in
different locations).
Now the method I envision would work as follows :
1) Do a non-validating pass with no schema specified.
2) Get the root element from the first pass, use it to determine the
actual message type and reparse/validate with the appropriate schema
specified.
Is this my best method, or is there a way to tell the parser (I intend
to use the DOMBuilder class) that there are multiple possible schemas
that the XML might parse to (differentiated by root element).
Thanks
Dale Pennington
Re: More than one Schema Possible ?
Posted by David Bertoni <db...@apache.org>.
EXT-Pennington, Dale K wrote:
> For background info, I am an experienced C/C++ developer, but rather new
> to XML.
>
> I have downloaded Xerces, build the samples, and have the basics down.
>
> The issue I am looking at is as follows :
>
...
>
> Now the method I envision would work as follows :
> 1) Do a non-validating pass with no schema specified.
> 2) Get the root element from the first pass, use it to determine the
> actual message type and reparse/validate with the appropriate schema
> specified.
>
> Is this my best method, or is there a way to tell the parser (I intend
> to use the DOMBuilder class) that there are multiple possible schemas
> that the XML might parse to (differentiated by root element).
If the different message types use unique XML namespaces, then you can use
the external schema location property:
http://xerces.apache.org/xerces-c/program-dom.html#DOMBuilderProperties
If the message don't use unique namespaces, then you will need to look at
the root element. Note that you can use the SAX2 parser, and stop parsing
after you see the root element.
Dave
Re: More than one Schema Possible ?
Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Boris,
Boris Kolpackov <bo...@codesynthesis.com> wrote on 10/29/2007 02:28:29 AM:
<snip/>
> So your two choices are to provide them directly in the document
> or as an attribute with default or fixed value in DTD or, maybe,
> XML Schema. I personally have never seen a namespace declaration
> done via a default attribute in XML Schema.
You haven't seen it because you can't do that [1][2] with XML Schema.
> Boris
>
> --
> Boris Kolpackov
> Code Synthesis Tools CC
> http://www.codesynthesis.com
> Open-Source, Cross-Platform C++ XML Data Binding
Thanks.
[1] http://www.w3.org/TR/xmlschema-1/#Attribute_Declaration_details
[2] http://www.w3.org/TR/xmlschema-1/#no-xmlns
Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org
Re: More than one Schema Possible ?
Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Dale,
Dale Worley <dw...@pingtel.com> writes:
> If I understand you correctly, XML does not permit the namespace of the
> root element to be implied by some context, it must be declared
> explicitly in the document?
The spec says[1] that
"A namespace (or more precisely, a namespace binding) is declared using
a family of reserved attributes. [...] These attributes, like any other
XML attributes, may be provided directly or by default."
So your two choices are to provide them directly in the document
or as an attribute with default or fixed value in DTD or, maybe,
XML Schema. I personally have never seen a namespace declaration
done via a default attribute in XML Schema.
[1] http://www.w3.org/TR/2006/REC-xml-names-20060816/#ns-decl
Boris
--
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding
Re: More than one Schema Possible ?
Posted by Dale Worley <dw...@pingtel.com>.
On Tue, 2007-10-23 at 16:52 +0200, Boris Kolpackov wrote:
> Your XML document is invalid per your schema.
If I understand you correctly, XML does not permit the namespace of the
root element to be implied by some context, it must be declared
explicitly in the document?
Dale
Re: More than one Schema Possible ?
Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Dale,
Dale Worley <dw...@pingtel.com> writes:
> I'm dealing with a similar problem -- the XML is known to be in a
> particular namespace, but the root element does not have an XML
> namespace declaration. I've got an .xsd file containing the schema for
> the namespace.
>
> Given that the underlying parser has a way of inheriting the default
> namespace from an element to its children, it seems like there must be a
> way of setting the "your default namespace" for the root element of the
> XML to be parsed.
Your XML document is invalid per your schema. If Xerces provided ways
to "work around" invalid documents, it would be hard to decide where
to stop. For example, if some broken system produced tag names in all
capital while the schema says they should be lower case, should there
be a feature to "handle" this?
The way I would handle your situation is like this: I would create
my own, vocabulary-specific input source which would detect root
elements without namespaces and "fix" them by injecting the missing
namespace declaration into the output. Because this all happens at
the root element level, the code should be pretty simple.
Boris
--
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding
Re: More than one Schema Possible ?
Posted by Dale Worley <dw...@pingtel.com>.
On Tue, 2007-09-18 at 22:18 +0200, Boris Kolpackov wrote:
> David Bertoni <db...@apache.org> writes:
>
> > You can use loadGrammar() with an empty string for the namespace URI
>
> Just a small correction: loadGrammar does not require one to pass
> the target namespace. Instead it gets this information from the
> schema itself.
I'm dealing with a similar problem -- the XML is known to be in a
particular namespace, but the root element does not have an XML
namespace declaration. I've got an .xsd file containing the schema for
the namespace.
The difficulty seems to be that there is no way to tell Xerces "use
*this* .xsd for validating no-namespace nodes", because Xerces will read
the namespace declaration in the .xsd file.
Given that the underlying parser has a way of inheriting the default
namespace from an element to its children, it seems like there must be a
way of setting the "your default namespace" for the root element of the
XML to be parsed.
Dale
Re: More than one Schema Possible ?
Posted by Boris Kolpackov <bo...@codesynthesis.com>.
David Bertoni <db...@apache.org> writes:
> You can use loadGrammar() with an empty string for the namespace URI
Just a small correction: loadGrammar does not require one to pass
the target namespace. Instead it gets this information from the
schema itself.
Boris
--
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding
Re: More than one Schema Possible ?
Posted by David Bertoni <db...@apache.org>.
Dale Worley wrote:
> On Mon, 2007-09-17 at 22:18 +0200, Boris Kolpackov wrote:
>> Hi Dale,
>>
>> Dale Worley <dw...@pingtel.com> writes:
>>
>>> What do you do if the root element does not specify the namespace, but
>>> the caller of Xerces knows what the namespace must be?
>> If the root element does not specify a namespace then this element
>> is not in any namespace. The declaration for such an element will
>> be searched in a grammar corresponding to a schema without a target
>> namespace.
>
> OK, how do I instruct Xerces that the grammar/schema without a target
> namespace is in the file named foo.xsd?
You can use loadGrammar() with an empty string for the namespace URI, or
use the property
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation".
See here for more information:
http://xerces.apache.org/xerces-c/program-dom.html#DOMBuilderProperties
Dave
Re: More than one Schema Possible ?
Posted by Dale Worley <dw...@pingtel.com>.
On Mon, 2007-09-17 at 22:18 +0200, Boris Kolpackov wrote:
> Hi Dale,
>
> Dale Worley <dw...@pingtel.com> writes:
>
> > What do you do if the root element does not specify the namespace, but
> > the caller of Xerces knows what the namespace must be?
>
> If the root element does not specify a namespace then this element
> is not in any namespace. The declaration for such an element will
> be searched in a grammar corresponding to a schema without a target
> namespace.
OK, how do I instruct Xerces that the grammar/schema without a target
namespace is in the file named foo.xsd?
Dale
Re: More than one Schema Possible ?
Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Dale,
Dale Worley <dw...@pingtel.com> writes:
> What do you do if the root element does not specify the namespace, but
> the caller of Xerces knows what the namespace must be?
If the root element does not specify a namespace then this element
is not in any namespace. The declaration for such an element will
be searched in a grammar corresponding to a schema without a target
namespace.
Boris
--
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding
Re: More than one Schema Possible ?
Posted by Dale Worley <dw...@pingtel.com>.
On Mon, 2007-09-17 at 12:38 -0700, David Bertoni wrote:
> Only one grammar per namespace URI, not one grammar. The parser uses the
> namespace URI of the root element to locate a grammar. The namespace URI
> string is a key into a hash table of grammar objects.
What do you do if the root element does not specify the namespace, but
the caller of Xerces knows what the namespace must be?
Dale
Re: More than one Schema Possible ?
Posted by David Bertoni <db...@apache.org>.
Dale Worley wrote:
> On Fri, 2007-09-14 at 20:24 +0200, Boris Kolpackov wrote:
>> // Initialize the schema cache.
>> //
>> parser->loadGrammar ("library.xsd", Grammar::SchemaGrammarType, true);
>> parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);
>
> What precisely is it that tells the parser that the XML is described by
> "library.xsd" even though there is no namespace reference in the XML
> itself? The setFeature call appears to do that, but there must be some
> implicit requirement that there be only one cached grammar at that
> moment, otherwise how would the parser know which grammar to apply?
Only one grammar per namespace URI, not one grammar. The parser uses the
namespace URI of the root element to locate a grammar. The namespace URI
string is a key into a hash table of grammar objects.
Dave
Re: More than one Schema Possible ?
Posted by Dale Worley <dw...@pingtel.com>.
On Fri, 2007-09-14 at 20:24 +0200, Boris Kolpackov wrote:
> // Initialize the schema cache.
> //
> parser->loadGrammar ("library.xsd", Grammar::SchemaGrammarType, true);
> parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);
What precisely is it that tells the parser that the XML is described by
"library.xsd" even though there is no namespace reference in the XML
itself? The setFeature call appears to do that, but there must be some
implicit requirement that there be only one cached grammar at that
moment, otherwise how would the parser know which grammar to apply?
Dale
Re: More than one Schema Possible ?
Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Dale,
EXT-Pennington, Dale K <da...@boeing.com> writes:
> Since I currently have multiple Schema files, would I call loadGrammar
> multiple times (1 per Schema file) or would I need to produce a new
> Schema file that was the union of the other Schema file ?
Hm, good questions. I just checked the caching code and it appears
that loadGrammar uses schema's target namespace as a key to decide
if a grammar for a particular namespace is already present. This
means that if you try to load two schemas that have the same target
namespace then the second schema will be ignored. This is easy to
overcome, however, by creating a "root" schema that includes all
other schemas for a particular namespace. You can then load that
schema and all included schema will be loaded automatically.
For schemas that use different namespaces you can simply call
loadGrammar for each file.
Boris
--
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding
RE: More than one Schema Possible ?
Posted by "EXT-Pennington, Dale K" <da...@boeing.com>.
> -----Original Message-----
> From: Boris Kolpackov [mailto:boris@codesynthesis.com]
> Sent: Friday, September 14, 2007 1:24 PM
> To: c-users@xerces.apache.org
> Subject: Re: More than one Schema Possible ?
>
> Hi Dale,
>
> EXT-Pennington, Dale K <da...@boeing.com> writes:
>
> > Now the method I envision would work as follows :
> > 1) Do a non-validating pass with no schema specified.
> > 2) Get the root element from the first pass, use it to
> determine the
> > actual message type and reparse/validate with the
> appropriate schema
> > specified.
>
> I think there is a better way to handle this. From your
> description I gather that you have different root element
> names for different messages. In this case you can load and
> cache the schemas for all possible message types. When the
> parser tries to validate an XML instance it will look for the
> corresponding root element grammar in the cache and use that
> to validate.
>
> The code for caching schemas looks along these lines:
>
> const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};
>
> // Get an implementation of the Load-Store (LS) interface.
> //
> DOMImplementation* impl (
> DOMImplementationRegistry::getDOMImplementation (ls_id));
>
> // Create a DOMBuilder.
> //
> xml::dom::auto_ptr<DOMBuilder> parser (
>
> impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0));
>
> // Discard comment nodes in the document.
> //
> parser->setFeature (XMLUni::fgDOMComments, false);
>
> // Enable datatype normalization.
> //
> parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true);
>
> // Do not create EntityReference nodes in the DOM tree.
> //
> parser->setFeature (XMLUni::fgDOMEntities, false);
>
> // Perform namespace processing.
> //
> parser->setFeature (XMLUni::fgDOMNamespaces, true);
>
> // Do not include ignorable whitespace in the DOM tree.
> //
> parser->setFeature
> (XMLUni::fgDOMWhitespaceInElementContent, false);
>
> // Enable validation.
> //
> parser->setFeature (XMLUni::fgDOMValidation, true);
> parser->setFeature (XMLUni::fgXercesSchema, true);
> parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
>
> // Initialize the schema cache.
> //
> parser->loadGrammar ("library.xsd",Grammar::SchemaGrammarType,
true);
> parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse,
true);
>
> It is taken from one of our examples and uses our
> dom::auto_ptr to simplify memory management but the overall
> idea is the same even if you don't use it.
>
> Boris
>
Since I currently have multiple Schema files, would I call loadGrammar
multiple times (1 per Schema file) or would I need to produce a new
Schema file that was the union of the other Schema file ?
Thanks
Dale
Re: More than one Schema Possible ?
Posted by Boris Kolpackov <bo...@codesynthesis.com>.
Hi Dale,
EXT-Pennington, Dale K <da...@boeing.com> writes:
> Now the method I envision would work as follows :
> 1) Do a non-validating pass with no schema specified.
> 2) Get the root element from the first pass, use it to determine the
> actual message type and reparse/validate with the appropriate schema
> specified.
I think there is a better way to handle this. From your description
I gather that you have different root element names for different
messages. In this case you can load and cache the schemas for all
possible message types. When the parser tries to validate an XML
instance it will look for the corresponding root element grammar
in the cache and use that to validate.
The code for caching schemas looks along these lines:
const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};
// Get an implementation of the Load-Store (LS) interface.
//
DOMImplementation* impl (
DOMImplementationRegistry::getDOMImplementation (ls_id));
// Create a DOMBuilder.
//
xml::dom::auto_ptr<DOMBuilder> parser (
impl->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0));
// Discard comment nodes in the document.
//
parser->setFeature (XMLUni::fgDOMComments, false);
// Enable datatype normalization.
//
parser->setFeature (XMLUni::fgDOMDatatypeNormalization, true);
// Do not create EntityReference nodes in the DOM tree.
//
parser->setFeature (XMLUni::fgDOMEntities, false);
// Perform namespace processing.
//
parser->setFeature (XMLUni::fgDOMNamespaces, true);
// Do not include ignorable whitespace in the DOM tree.
//
parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false);
// Enable validation.
//
parser->setFeature (XMLUni::fgDOMValidation, true);
parser->setFeature (XMLUni::fgXercesSchema, true);
parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);
// Initialize the schema cache.
//
parser->loadGrammar ("library.xsd", Grammar::SchemaGrammarType, true);
parser->setFeature (XMLUni::fgXercesUseCachedGrammarInParse, true);
It is taken from one of our examples and uses our dom::auto_ptr to
simplify memory management but the overall idea is the same even if
you don't use it.
Boris
--
Boris Kolpackov
Code Synthesis Tools CC
http://www.codesynthesis.com
Open-Source, Cross-Platform C++ XML Data Binding