XML Schema Q: Element with either simpleContent or complexContent

Discussion in 'XML' started by David Norman, Jul 31, 2004.

  1. David Norman

    David Norman Guest

    How can I create an xml schema where both of the following documents
    are valid?

    <theElement>
    <child>some text</child>
    </theElement>

    and

    <theElement>1234</theElement>

    Basically, <theElement> should either contain a single element <child>
    of a specified type or an integer.

    Thanks,
    David
     
    David Norman, Jul 31, 2004
    #1
    1. Advertising

  2. David Norman wrote:

    > How can I create an xml schema where both of the following documents
    > are valid?
    >
    > <theElement>
    > <child>some text</child>
    > </theElement>
    >
    > and
    >
    > <theElement>1234</theElement>
    >
    > Basically, <theElement> should either contain a single element <child>
    > of a specified type or an integer.



    If you use

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

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

    <xs:element name="theElement">
    <xs:complexType mixed="true">
    <xs:sequence>
    <xs:element name="child" type="xs:string" minOccurs="0" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

    that is you specify the element to have a complex type where mixed
    content is allowed then the following validates:

    <?xml version="1.0" encoding="UTF-8"?>
    <root
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test2004073101Xsd.xml">
    <theElement>
    <child>some text</child>
    </theElement>
    <theElement>1234</theElement>
    </root>

    However the schema also allows a <theElement> as follows
    <theElement>1234<child>Kibology</child>1234</theElement>

    If you want to allow either the simple content of type integer or a
    complex content of exactly one child element you would need to have a
    union of both types however I think a union is only possible for simple
    types.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 31, 2004
    #2
    1. Advertising

  3. David Norman

    David Norman Guest

    Martin Honnen <> wrote in message news:<>...
    > David Norman wrote:
    >
    > > How can I create an xml schema where both of the following documents
    > > are valid?
    > >
    > > <theElement>
    > > <child>some text</child>
    > > </theElement>
    > >
    > > and
    > >
    > > <theElement>1234</theElement>
    > >
    > > Basically, <theElement> should either contain a single element <child>
    > > of a specified type or an integer.

    >
    >
    > If you use
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > version="1.0">
    >
    > <xs:element name="root">
    > <xs:complexType>
    > <xs:sequence>
    > <xs:element ref="theElement" maxOccurs="unbounded" />
    > </xs:sequence>
    > </xs:complexType>
    > </xs:element>
    >
    > <xs:element name="theElement">
    > <xs:complexType mixed="true">
    > <xs:sequence>
    > <xs:element name="child" type="xs:string" minOccurs="0" />
    > </xs:sequence>
    > </xs:complexType>
    > </xs:element>
    >
    > </xs:schema>
    >
    > that is you specify the element to have a complex type where mixed
    > content is allowed then the following validates:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <root
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:noNamespaceSchemaLocation="test2004073101Xsd.xml">
    > <theElement>
    > <child>some text</child>
    > </theElement>
    > <theElement>1234</theElement>
    > </root>
    >
    > However the schema also allows a <theElement> as follows
    > <theElement>1234<child>Kibology</child>1234</theElement>
    >
    > If you want to allow either the simple content of type integer or a
    > complex content of exactly one child element you would need to have a
    > union of both types however I think a union is only possible for simple
    > types.


    Because of the last possibility, mixed="true" doesn't give me what I want.

    Thanks,
    David
     
    David Norman, Aug 2, 2004
    #3
  4. David Norman

    Peter Flynn Guest

    David Norman wrote:

    > How can I create an xml schema where both of the following documents
    > are valid?
    >
    > <theElement>
    > <child>some text</child>
    > </theElement>
    >
    > and
    >
    > <theElement>1234</theElement>
    >
    > Basically, <theElement> should either contain a single element <child>
    > of a specified type or an integer.


    This sounds to me like extremely poor data design.

    ///Peter
    --
    "The cat in the box is both a wave and a particle"
    -- Terry Pratchett, introducing quantum physics in _The Authentic Cat_
     
    Peter Flynn, Aug 4, 2004
    #4
    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. Random
    Replies:
    1
    Views:
    4,672
    Random
    Nov 19, 2004
  2. Markus
    Replies:
    1
    Views:
    1,622
    Markus
    Nov 23, 2005
  3. Swaroop Kumar
    Replies:
    0
    Views:
    402
    Swaroop Kumar
    Aug 19, 2003
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,301
    Stanimir Stamenkov
    Apr 25, 2005
  5. Kai Schlamp
    Replies:
    3
    Views:
    530
    Pavel Lepin
    Nov 1, 2007
Loading...

Share This Page