Can I restrict both attribute contents and element contents in schema

Discussion in 'XML' started by Don Adams, Mar 4, 2004.

  1. Don Adams

    Don Adams Guest

    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
    Don Adams, Mar 4, 2004
    #1
    1. Advertising

  2. Re: Can I restrict both attribute contents and element contents inschema

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

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 5, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,518
    Markus
    Nov 23, 2005
  2. Piers Chivers
    Replies:
    0
    Views:
    484
    Piers Chivers
    Feb 18, 2004
  3. tankbattle
    Replies:
    1
    Views:
    790
    George Bina
    Jul 21, 2006
  4. Eric
    Replies:
    4
    Views:
    1,081
    Stan Kitsis [MSFT]
    Mar 7, 2007
  5. Alan Johnson
    Replies:
    0
    Views:
    377
    Alan Johnson
    Jul 24, 2007
Loading...

Share This Page