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

B

burkley

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?
 
G

George Bina

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
 
P

Priscilla Walmsley

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
 
U

usenet

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/
=============================================
 
P

Priscilla Walmsley

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
 
B

burkley

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

Thanks for the discussion on this.

I've experimented / digested a bit and have the following observations
(with example code for each):

Background is I have a large XSD that I want to subset via complex
type restriction, with the restricted types defined in a namespace
different than the namespace of the XSD I am subsetting. For
discussion, lets say there is a "base" namespace and a "restricted"
namespace. The restricted types will be defined in the "restricted"
namespace.

I (at present) see 2 approaches:

1) The base namespace has global element and complex type definitions,
however the global complex types have some local element definitions.

For the types in the restricted namespace, I need to repeat the
mandatory content of the types in the base namespace that I am
restricting. However, I can't really "refer" to the mandatory content
of the base types since some of that content is local in the base
namespace.

So... I can say elementFormDefault="unqualified" in both the base XSD
and restricted XSD, and can then create an instance document
containing the restricted type, whose content is the mandatory content
of the base namespace, and the element names are unqualified.

<?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/NewXMLSchemaExtension
RestrictionWithLocalElementDefs.xsd ">
<E1>E1</E1>
</res:ARestricted>



2) The base namespace has ALL global element and complex type
definitions; there are no local element definitions or anonymous
complex types. I believe this is the "salami slice" design pattern.

For the types in the restricted namespace, I need to repeat the
mandatory content of the types in the base namespace that I am
restricting. Since all elements have global definitions, I can
"refer" to them via a ref.

So... I can say elementFormDefault="qualified" in both the base XSD
and restricted XSD, and can then create an instance document
containing the restricted type, whose content is the mandatory content
of the base namespace, and the element names are qualified.

<?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/NewXMLSchemaExtension
NewXMLSchemaRestriction.xsd ">
<base:E1>E1</base:E1>
</res:ARestricted>



I much perfer option 2 since all elements are prefixed with
appropriate namespace. This gets to the rationale of many of the
Naming and Design Rules I think. I'm not an expert on NDRs, however,
defining all elements and types globally means (among other things)
that I can subset/restrict to a different namespace and have all
resulting elements prefixed with appropriate namespace.


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

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

<?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/NewXMLSchemaExtension
RestrictionWithLocalElementDefs.xsd ">
<E1>E1</E1>
</res:ARestricted>

----------------------------------------------
XDSs for option 2
----------------------------------------------

-- Base 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"
xmlns:tns="http://www.example.org/NewXMLSchema"
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

<?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/NewXMLSchemaExtension
NewXMLSchemaRestriction.xsd ">
<base:E1>E1</base:E1>
</res:ARestricted>
 
U

usenet

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/
=============================================
 
B

burkley

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>
 

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
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top