How to write an XML schema that specifies an optional namespace inthe XML docs?

  • Thread starter Jethrie-JDuprez in the news
  • Start date
J

Jethrie-JDuprez in the news

Hello,

I have to support an application which among other things consumes XML
documents. There is a requirement that may be unusual, although I
haven't detected it soon enough for lack of XML expertise. I submitted
my question to a developper forum, as the XML processing is written in
Java, but I suspect the problem is not in any java library but may be
that the requirement is not compliant with XML standards. Here it is:

We have a requirement to process incoming XML documents that may or
may not have a namespace specified.
That is, the textual specification explicitly says that incoming
document may be of the following two forms:

<DeliveryReport xmlns="http://my.company.com/schema/
DeliveryReport.xsd" >
<Item ref="xxx" quantity="25"/>
<Item ref="yyy" quantity="12"/>
</DeliveryReport>

<DeliveryReport>
<Item ref="xxx" quantity="25"/>
<Item ref="yyy" quantity="12"/>
</DeliveryReport>


As you can see, one of the form has a namespace specifier, while the
other has no namespace.

I don't know how to translate this requirement into an XML Schema:
either I specify a targetnamespace attribute, or I don't. Either way,
one of the forms of incoming documents will not pass the validation
performed by the JAXB XML library.

The solution (I meant, ugly hack) so far was to intercept incoming
docs and replace on-the-fly occurences of "<DeliveryReport>" for the
qualified form.

Is there some way, in XSD syntax, to specify that the namespace is not
mandatory in the XML documents?

Thank you in advance.
J.
 
M

Martin Honnen

Jethrie-JDuprez in the news said:
Is there some way, in XSD syntax, to specify that the namespace is not
mandatory in the XML documents?

No, but you can usually validate against a set of schemas meaning you
could provide both schemas to your parser and it would then choose the
right one based on the presence or absence of the namespace on the root
element of the XML instance document.
 
J

Jethrie-JDuprez in the news

No, but you can usually validate against a set of schemas meaning you
could provide both schemas to your parser and it would then choose the
right one based on the presence or absence of the namespace on the root
element of the XML instance document.

Thank you for taking the time.
This drawbacks of this approach are:
- the schema (or merely, the textual specification of the incoming XML
documents) is bound to change a few times a year. If we maintain 2
schemas I fear some day or the other someone will forget to keep both
schemas in sync.
- I haven't found a way to specify more than one schemas to the JAXB
library, but that is specific to this API.

I will try to lobby the customer into choosing only one of the forms,
but I suspect some provider of the XMLs at the other end will
complain...
Best regards,

J.
 
J

Joe Kesselman

You can write the schema to accept both the namespaced and
non-namespaced versions, but that does mean actively maintaining both
alternatives in parallel.

If you're willing to demand a DTD as well as a schema, you could have
the DTD assert a default namespace declaration as a default attribute
value on some or all of the elements. But that cab mess up the user's
attempts to work with default namespaces in instance documents. Fragile,
not recommended.

Or you could preprocess the document to replace the un-namespaced
versions with namespaced equivalents, eg by using an XSLT stylesheet. Of
course that means more code everywhere you want to work with these
documents.

But really, the right answer here is that the namespace is part of the
name of the namespaced elements and attributes. If you remove it, you've
changed the meaning of those names. You probably don't really want to do
this. Especially since at this point everyone working with XML should be
used to working with namespaces -- or should get used to them -- so
you're doing a lot of work to avoid a problem that people shouldn't be
provoking.

Modern XML processing is namespace-aware. Get used to it.
 
M

Mayeul

Jethrie-JDuprez in the news said:
Thank you for taking the time.
This drawbacks of this approach are:
- the schema (or merely, the textual specification of the incoming XML
documents) is bound to change a few times a year. If we maintain 2
schemas I fear some day or the other someone will forget to keep both
schemas in sync.

If you were to go for this method, you would just need to think of it
differently.

You don't need to keep both schemas in sync. Suffice to maintain just
one schema (say, the one with namespaces,) and to produce the other one
from it. Whether the schema uses a namespace is only little work at the
head of the file, after all. "Producing" the other version would be a
simple matter of "save as..." Another name, then change the head of the
file to make it a schema with no namespaces.



That's if you were to choose this method. As Joe mentioned, XML and the
tools to use it are now namespace-aware, and a choice should be made on
whether to have a namespace or not. Keeping both alternatives is not meant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top