Required and optional elements

Discussion in 'XML' started by Spaulding, Aug 14, 2009.

  1. Spaulding

    Spaulding Guest

    I have an xsd specified as such:

    <?xml version="1.0" encoding="UTF-8" ?>
    - <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    - <xs:element name="REQUEST">
    - <xs:complexType>
    - <xs:all>
    <xs:element name="FIELD1" type="xs:string" />
    <xs:element name="FIELD2" type="xs:string" />
    <xs:element name="FIELD3" type="xs:string" minOccurs="0" />
    </xs:all>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    I was thinking that this would allow FIELD3 to be optional and that
    FIELD1 and FIELD2 are required. I use JAXB to unmarshal from an XML
    doc that does not have FIELD1, and it works. i would have expected an
    exception. Am i doing this correctly?
    Spaulding, Aug 14, 2009
    #1
    1. Advertising

  2. Spaulding wrote:
    > I have an xsd specified as such:
    >
    > <?xml version="1.0" encoding="UTF-8" ?>
    > - <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > elementFormDefault="qualified" attributeFormDefault="unqualified">
    > - <xs:element name="REQUEST">
    > - <xs:complexType>
    > - <xs:all>
    > <xs:element name="FIELD1" type="xs:string" />
    > <xs:element name="FIELD2" type="xs:string" />
    > <xs:element name="FIELD3" type="xs:string" minOccurs="0" />
    > </xs:all>
    > </xs:complexType>
    > </xs:element>
    > </xs:schema>
    >
    > I was thinking that this would allow FIELD3 to be optional and that
    > FIELD1 and FIELD2 are required. I use JAXB to unmarshal from an XML
    > doc that does not have FIELD1, and it works. i would have expected an
    > exception. Am i doing this correctly?


    In terms of the schema language one FIELD1 child element of the REQUEST
    element is required and a validating parser should flag that as an error
    if there is no such element.
    Whether JAXB validates I don't know. You might want to ask in a Java group.

    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
    Martin Honnen, Aug 15, 2009
    #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. Sean
    Replies:
    8
    Views:
    384
    Mark Parnell
    Feb 15, 2004
  2. MENTAT
    Replies:
    8
    Views:
    944
    MENTAT
    Apr 4, 2005
  3. LJ
    Replies:
    3
    Views:
    1,121
    George Bina
    Feb 9, 2006
  4. Thomas Wittek
    Replies:
    4
    Views:
    1,458
    Joe Kesselman
    Sep 13, 2007
  5. Philipp
    Replies:
    2
    Views:
    520
    Philipp
    Apr 11, 2008
Loading...

Share This Page