By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,314 Members | 1,745 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,314 IT Pros & Developers. It's quick & easy.

XSD type derived via restriction in same namespace as restricted type?

P: n/a
In XML Schema, is it possible to derive a complex type via restriction
and have the new derived type be in a different namespace than the
original base type?

I've banged on this for 2 days now and I'm starting to think the
answer is no. I've searched this group and have not seen anything
discussing this.
>From Walmsley: "The values for the new type are a subset of those for
the base type. All values of the restricted type are also valid
according to the base type".

Now since all values of the restricted type are also valid according
to the base type, it follows that the namespace of the derived type
must be the same as the namespace of the base type. Otherwise the
instances of the derived type would not conform to the base type.

My question: Is it possible to derive a complex type via restriction
and have the new derived type be in a different namespace than the
original base type?

Sep 16 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Yes, it is possible.
The thing that you should take into account is that XML Schema
requires a different file to define a different namespace, so you
cannot derive the type and have it in a different namespace in the
same file as the base type.

Here you have an example:

base.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.oxygenxml.com/ns/base">
<xs:simpleType name="test">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

restricted.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.oxygenxml.com/ns/restricted"
xmlns:base="http://www.oxygenxml.com/ns/base">
<xs:import namespace="http://www.oxygenxml.com/ns/base"
schemaLocation="base.xsd"/>

<xs:simpleType name="restrictedTest">
<xs:restriction base="base:test">
<xs:enumeration value="val1"/>
<xs:enumeration value="val2"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina - http://aboutxml.blogspot.com/
<oXygen/XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Sep 17 '07 #2

P: n/a
Hi,

If you're talking about complex types, the type name itself can be in a
different namespace. But the child element names must be in the same
namespace (or no namespace in both types.)

So, if you are using elementFormDefault="qualified", or you are using
the "ref" attribute to refer to globally declared elements, it won't
work to have the derived type in another namespace.

If you are using elementFormDefault="unqualified" and all your element
declarations are local, it can work.

Hope that helps,
Priscilla

-----------------------------------------------
Priscilla Walmsley
Author, Definitive XML Schema (2001, Pren Hall)
XQuery (2007, O'Reilly Media)
http://www.datypic.com
http://www.xqueryfunctions.com
-----------------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Sep 17 '07 #3

P: n/a
On 17 Sep, 15:38, Priscilla Walmsley <nos...@datypic.comwrote:
Hi,

If you're talking about complex types, the type name itself can be in a
different namespace. But the child element names must be in the same
namespace (or no namespace in both types.)

So, if you are using elementFormDefault="qualified", or you are using
the "ref" attribute to refer to globally declared elements, it won't
work to have the derived type in another namespace.
Hi Priscilla,

Taking the case where the elements used the ref='' variant, would it
be OK in the above case to have the elements of the restricted type
echo the elements of the base type, and then restrict the attributes
associated with the complex type (assuming
attributeFormDefault='false')?
If you are using elementFormDefault="unqualified" and all your element
declarations are local, it can work.
Cheers,

Pete.
=============================================
Pete Cordell
Codalogic
for XML Schema to C++ data binding visit
http://codalogic.com/lmx/
=============================================

Sep 17 '07 #4

P: n/a
Hi Pete,

Oh yes, sure - I didn't think of that case. If you are "ref"-ing the
element declarations that are in the original namespace, it's OK (they
can be the same child elements or a restricted set of those child
elements).

Its just if you were "ref"-ing element in the new namespace that would
cause a problem.

And yes, it works in a parallel way for attributes. If you use
attributeFormDefault="unqualified" (the default behavior), you're fine.
If you're using attributes in namespaces, the attributes have to be in
the same namespace in the restricted type.

Thanks,
Priscilla

---------------------------------------------
Priscilla Walmsley
Author, XQuery (2007, O'Reilly Media)
http://www.datypic.com
http://www.xqueryfunctions.com
---------------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Sep 17 '07 #5

P: n/a
On 17 Sep, 17:21, Priscilla Walmsley <nos...@datypic.comwrote:
Hi Pete,

Oh yes, sure - I didn't think of that case. If you are "ref"-ing the ...
Thanks Priscilla. I'll admit it was a pretty contrived case, and
probably not that useful, but I wanted to check my understanding.

Cheers,

Pete.
=============================================
Pete Cordell
Codalogic
for XML Schema to C++ data binding visit
http://www.codalogic.com/lmx/
=============================================

Sep 18 '07 #6

P: n/a
I made a few mistakes in my example XSDs/instances. I'm re-posting
FYI. I welcome any comments.

----------------------------------------------
XDSs for option 1
----------------------------------------------

-- Base XSD, filename BaseWithLocalElementDefs.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.org/Base"
targetNamespace="http://www.example.org/Base"
elementFormDefault="unqualified">

<xs:element name="A" type="AType" />
<xs:complexType name="AType">
<xs:sequence>
<xs:element name="E1" type="xs:string" />
<xs:element name="E2" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>

-- Restricting XSD, with restricted types in different namespace.
filename RestrictionWithLocalElementDefs.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.org/Restriction"
targetNamespace="http://www.example.org/Restriction"
xmlns:base="http://www.example.org/Base"
elementFormDefault="unqualified">

<xs:import namespace="http://www.example.org/Base"
schemaLocation="BaseWithLocalElementDefs.xsd" />

<xs:element name="ARestricted" type="ARestrictedType" />
<xs:complexType name="ARestrictedType">
<xs:complexContent>
<xs:restriction base="base:AType">
<xs:sequence>
<xs:element name="E1" type="xs:string" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>

-- Instance

<?xml version="1.0" encoding="UTF-8"?>
<res:ARestricted xmlns:res="http://www.example.org/Restriction"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/Restriction
RestrictionWithLocalElementDefs.xsd ">
<E1>E1</E1>
</res:ARestricted>
----------------------------------------------
XDSs for option 2
----------------------------------------------

-- Base XSD, filename BaseWithGlobalElementDefs.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.org/Base"
targetNamespace="http://www.example.org/Base"
elementFormDefault="qualified">

<xs:element name="E1" type="E1Type" />
<xs:simpleType name="E1Type">
<xs:restriction base="xs:string" />
</xs:simpleType>

<xs:element name="E2" type="E2Type" />
<xs:simpleType name="E2Type">
<xs:restriction base="xs:string" />
</xs:simpleType>

<xs:element name="A" type="AType" />
<xs:complexType name="AType">
<xs:sequence>
<xs:element ref="E1" />
<xs:element ref="E2" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:schema>

-- Restricting XSD, filename RestrictionWithGlobalElementDefs.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.org/Restriction"
targetNamespace="http://www.example.org/Restriction"
xmlns:base="http://www.example.org/Base"
elementFormDefault="qualified">

<xs:import namespace="http://www.example.org/Base"
schemaLocation="BaseWithGlobalElementDefs.xsd" />

<xs:element name="ARestricted" type="ARestrictedType" />
<xs:complexType name="ARestrictedType">
<xs:complexContent>
<xs:restriction base="base:AType">
<xs:sequence>
<xs:element ref="base:E1" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>

-- Instance

<?xml version="1.0" encoding="UTF-8"?>
<res:ARestricted xmlns:res="http://www.example.org/Restriction"
xmlns:base="http://www.example.org/Base"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/Restriction
RestrictionWithGlobalElementDefs.xsd ">
<base:E1>E1</base:E1>
</res:ARestricted>
Sep 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.