Can I restrict both attribute contents and element contents in schema

D

Don Adams

I would like to have the following XML:
<phone type="work">555-123-1234</phone>
<phone type="home">555-123-4321</phone>

Is it possible to write a schema to restrict the contents of
the type attribute to be enumerated ("work" or "home") AND restrict the
contents of the <phone> element to match the pattern \d\d\d-\d\d\d-\d\d\d\d
?

I know how to do one or the other, but I haven't figured out a way
to do both. It seems that the pattern restriction can only be
applied to a simple type, but a simple type cannot have attributes. So
what I want to do may not be possible. Is this correct?
 
M

Martin Honnen

Don said:
I would like to have the following XML:
<phone type="work">555-123-1234</phone>
<phone type="home">555-123-4321</phone>

Is it possible to write a schema to restrict the contents of
the type attribute to be enumerated ("work" or "home") AND restrict the
contents of the <phone> element to match the pattern \d\d\d-\d\d\d-\d\d\d\d
?

I know how to do one or the other, but I haven't figured out a way
to do both. It seems that the pattern restriction can only be
applied to a simple type, but a simple type cannot have attributes. So
what I want to do may not be possible. Is this correct?


Here is a schema that defines your element phone and an element root to
contain a sequence of phone elements

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

<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="phone" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="phone">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="phoneNo">
<xs:attribute name="type" type="homeWork" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

<xs:simpleType name="phoneNo">
<xs:restriction base="xs:string">
<xs:pattern value="\d{3}-\d{3}-\d{4}" />
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="homeWork">
<xs:restriction base="xs:string">
<xs:enumeration value="home" />
<xs:enumeration value="work" />
</xs:restriction>
</xs:simpleType>

</xs:schema>
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top