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

Joined
Apr 14, 2009
Messages
2
Reaction score
0
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.
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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.
 
Joined
Apr 14, 2009
Messages
2
Reaction score
0
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.
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
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.
 

Mayeul

Usenet Archive
Joined
Oct 16, 2008
Messages
159
Reaction score
0
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.
 

C. M. Sperberg-McQueen

Usenet Archive
Joined
Jul 2, 2003
Messages
95
Reaction score
0
Jethrie-JDuprez in the news writes:

On 14 avr, 17:42, Martin Honnen wrote:

>> 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.

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 ...

One way to reduce the risk would be to exploit what is sometimes called 'chameleon include', to include the same schema document twice, once to generate unqualified components and once to generate components in the 'optional' target namespace.

To illustrate:

In document implicit_ns_1.xsd, declare no target namespace and define all the things you want. For examle:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" > <xsd:annotation> <xsd:documentation> <div xmlns="http://www.w3.org/1999/xhtml"> <p>Sample schema for discussion and testing purposes. 2 July 2009. </p> <p>This is the 'main' schema document, which does all the real work of defining types and elements.</p> </div> </xsd:documentation> </xsd:annotation>

<xsd:element name="sample"/>
<xsd:element name="x" type="dummy_type"/>
<xsd:element name="y" type="dummy_type"/>
<xsd:element name="z" type="dummy_type"/>

<xsd:complexType name="dummy_type" mixed="true">
<xsd:sequence>
<xsd:any processContents="strict" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType>

</xsd:schema>

Then in a driver file (say, implicit_ns.xsd), both include and import it:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/ns"
elementFormDefault="qualified" >
<annotation>
<documentation>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>Sample schema for discussion and testing purposes. 2 July 2009. </p> <p>This is the 'driver' schema document, which (a) imports the declarations using unqualified names, and (b) includes the declarations using 'chameleon include', so they also show up in the http://example.com/ns namespace.</p> </div> </documentation> </annotation>

<!--* perform a chameleon include, to get all the
* definitions in the 'main' schema document
* as components in the target namespace. *-->
<include schemaLocation="implicit_ns_1.xsd" />

<!--* perform an import with no namespace specified,
* to get all the definitions in the 'main' schema
* as components with unqualified names. *-->
<import schemaLocation="implicit_ns_1.xsd"/>

</schema>

The result is a schema with elements and types defined both in the given namespace and without any namespace.

I hope this helps.
 

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

No members online now.

Forum statistics

Threads
548,051
Messages
3,104,251
Members
248,135
Latest member
tobystanx

Latest Threads

Top