Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How to write an XML schema that specifies an optional namespace in the XML docs?

From: cmsmcq@---.--- (-. -. ----------------)
To: NULL
Date: 7/2/2009 8:12:00 PM
Jethrie-JDuprez in the news <Jethrie@g...> writes:

> On 14 avr, 17:42, Martin Honnen <mahotr...@yahoo.de> 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
> schemas I fear some day or the other someone will forget to keep both
> schemas in sync.

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.

-- 
****************************************************************
* C. M. Sperberg-McQueen, Black Mesa Technologies LLC
* http://www.blackmesatech.com
* http://cmsmcq.com/mib
* http://balisage.net
****************************************************************


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent