Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > W3C Schema for required and optional elements

Reply
Thread Tools

W3C Schema for required and optional elements

 
 
Philipp
Guest
Posts: n/a
 
      04-10-2008
Hello,
I'm learning W3C Schema 1.0 and have encountered a problem. I want to
make a complex type which requires to contain exactly 1 instance of each
of a list of elements (a, b, c) and at least once a choice of several
other elements ( d1 | d2 | d3 ). This in any order.

I have come up with the following which is (unfortunately) invalid XSD 1.0:
<complexType name="root">
<all>
<element name="a" type="string"></element>
<element name="b" type="string"></element>
<element name="c" type="string"></element>
<choice maxOccurs="unbounded" minOccurs="1">
<element name="d1" type="string"></element>
<element name="d2" type="string"></element>
<element name="d3" type="string"></element>
</choice>
</all>
</complexType>

If somebody can help me make up a valid schema for this, I would be very
thankful. Below are some XML examples to clarify what I expected.

Thanks Phil

=== XML example ===

VALID (a,b,c are there, and at least one d*)
<root>
<a />
<b />
<c />
<d1 />
</root>

VALID (order changed, that's OK)
<root>
<a />
<d1 />
<c />
<b />
</root>

VALID (multiple d* are OK)
<root>
<a />
<d2 />
<b />
<c />
<d3 />
<d2 />
</root>

INVALID (a appears twice, bad)
<root>
<a />
<b />
<c />
<d1 />
<a />
</root>

INVALID (no d* element, bad)
<root>
<a />
<b />
<c />
</root>

 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      04-11-2008

Philipp <> wrote in
<>:
> I'm learning W3C Schema 1.0 and have encountered a
> problem. I want to make a complex type which requires to
> contain exactly 1 instance of each of a list of elements
> (a, b, c) and at least once a choice of several other
> elements ( d1 | d2 | d3 ). This in any order.
>
> I have come up with the following which is (unfortunately)
> invalid XSD 1.0: <complexType name="root">
> <all>
> <element name="a" type="string"></element>
> <element name="b" type="string"></element>
> <element name="c" type="string"></element>
> <choice maxOccurs="unbounded" minOccurs="1">
> <element name="d1" type="string"></element>
> <element name="d2" type="string"></element>
> <element name="d3" type="string"></element>
> </choice>
> </all>
> </complexType>


W3C's schema definition language was not designed for
defining arbitrarily complex grammars. If your document
format is well-structured, expressing it in XML Schema
should be easy. If it's not, try using a more powerful
schema definition language or check validity on the
application side.

--
"...a Netscape engineer who shan't be named once passed a
pointer to JavaScript, stored it as a string and later
passed it back to C, killing 30..." --Blake Ross
 
Reply With Quote
 
 
 
 
Philipp
Guest
Posts: n/a
 
      04-11-2008
Pavel Lepin wrote:
> Philipp <> wrote in
> <>:
>> I'm learning W3C Schema 1.0 and have encountered a
>> problem. I want to make a complex type which requires to
>> contain exactly 1 instance of each of a list of elements
>> (a, b, c) and at least once a choice of several other
>> elements ( d1 | d2 | d3 ). This in any order.
>>
>> I have come up with the following which is (unfortunately)
>> invalid XSD 1.0: <complexType name="root">
>> <all>
>> <element name="a" type="string"></element>
>> <element name="b" type="string"></element>
>> <element name="c" type="string"></element>
>> <choice maxOccurs="unbounded" minOccurs="1">
>> <element name="d1" type="string"></element>
>> <element name="d2" type="string"></element>
>> <element name="d3" type="string"></element>
>> </choice>
>> </all>
>> </complexType>

>
> W3C's schema definition language was not designed for
> defining arbitrarily complex grammars. If your document
> format is well-structured, expressing it in XML Schema
> should be easy. If it's not, try using a more powerful
> schema definition language or check validity on the
> application side.
>


After some reading on the web, I think my biggest problem is that my
elements are non-ordered. This makes sense for me both from the model
perspective (I don't care if eg. my Book object sets the Author or the
Title first) as well as from my implementation perspective (these values
are marshalled from a java Properties object which does not guaratee a
constant traversal order for its iterator.

Hopefully Xerces for Schema 1.1 will soon be available (and also
that Schema 1.1 fixes these shortcommings)

Phil
 
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
Required and optional elements Spaulding XML 1 08-15-2009 10:14 AM
Castorizing W3C's Schema Schema virmundi@gmail.com Java 1 05-18-2006 05:28 AM
w3c Schema naming patterns and template-based schema generation Steve Jorgensen XML 0 08-09-2005 08:05 AM
XSD question: Allowing one required element and many optional elements. MENTAT XML 8 04-04-2005 04:16 AM
W3C Validator Icon and W3C Valdiator page Frank HTML 9 05-03-2004 09:32 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57