Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: How to write an XML schema that specifies an optional namespace in the XML docs? [Thread Next] Re: How to write an XML schema that specifies an optional namespace in the XML docs?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
****************************************************************
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
