Required and optional elements

Spaulding

Usenet Archive
Joined
Aug 14, 2009
Messages
1
Reaction score
0
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?
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Spaulding said:
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.
 

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

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top