Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Can I restrict both attribute contents and element contents in schema

Reply
Thread Tools

Can I restrict both attribute contents and element contents in schema

 
 
Don Adams
Guest
Posts: n/a
 
      03-04-2004

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?

--
Don Adams


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-05-2004


Don Adams wrote:

> 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 xmlnss="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">
<xsattern 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>
--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Schema element contain text or elements, but not both. Alan Johnson XML 0 07-24-2007 09:16 PM
XML Schema (XSD): allow an attribute OR a sub-element NOT BOTH Eric XML 4 03-07-2007 11:16 PM
In xml schema, what's the difference between attribute "final" and "block" in element "element" tankbattle XML 1 07-21-2006 07:51 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
How to restrict elements by reference to another element using XML schema Piers Chivers XML 0 02-18-2004 11:17 AM



Advertisments