Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restricting complex types in a schema

Status
Not open for further replies.

arandomdan

Programmer
Sep 22, 2008
1
US
I'm trying to restrict the content of an element and its attributes. Here is the relevant portion of my schema where the problem is located:

<xs:element name="name">
<xs:complexType mixed="true">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="title" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Mr."/>
<xs:enumeration value="Mrs."/>
<xs:enumeration value="Ms."/>
<xs:enumeration value="Miss"/>
<xs:enumeration value="Dr."/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

This only partly accomplishes my goal. The attribute "title" is all set up the way I need it with the appropriate restrictions placed on it. The "name" element, on the other hand, is only restricted to being a string, but I also need to make sure that it's only composed of alphanumeric characters. This needs to be done while keeping the restrictions I have on the title attribute.

I have tried everything (that I could think of) and any restriction I try to put on the "name" element's content results in an invalid schema.
 
[0] The element of the type is not considered as having mixed content type.

[1] To do extension then restriction, you've to do it through an intermediate type which can be made "abstract" if so desired. You cannot do it in one plot.

[1.1] First you defined the title attribute.
[tt]
<xs:simpleType name="titletype">
<xs:restriction base="xs:string">
<xs:enumeration value="Mr."/>
<xs:enumeration value="Mrs."/>
<xs:enumeration value="Ms."/>
<xs:enumeration value="Miss"/>
<xs:enumeration value="Dr."/>
</xs:restriction>
</xs:simpleType>
[/tt]
[1.2] Then you construct an intermediate type (which can be made abstract; or not abstract if you allow it to be a type of some other element which can be appeared in the instance document).
[tt]
<xs:complexType name="nametype_abstract" abstract="true">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="title" use="required" type="titletype" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
[/tt]
[1.3] Finally the complete nametype can be constructed with alphanumeric pattern.
[tt]
<xs:complexType name="nametype">
<xs:simpleContent>
<xs:restriction base="nametype_abstract">
<xs:pattern value="[a-zA-Z0-9]*" />
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
[/tt]
[1.3.1] The "alphanumric" so tightly defined restrict the name text to be something like this.

[tt] <name title="Mr.">JohnSmith</name>[/tt]

If you want in fact to allow something like these as well:

[tt] <name>John Smith</name>
<name>
John Smith
</name>
[/tt]

you've to change the pattern to this.

[tt] <xs:pattern value="\s*[a-zA-Z0-9[highlight] [/highlight]]*\s*" />[/tt]

[3] The the element "name" can be defined like this.
[tt]
<xs:element name="name" type="nametype" />
[/tt]
[4] The element name will be referemced in its parent container or elsewhere local like this.
[tt]
<xs:element ref="name" />
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top