Damiano ALBANI wrote:
Hello,
I'd like to write a single XML Schema for both these documents :
....
How about a schema that declares:
- Items elements to contain any number of Item elements (maybe any
number 0, up to you)
- Item elements to have an optional id attribute, an optional ref
attribute and a sequence of Name sub-elements of length 0 or 1
That looks "easy", but I've been googling for hours and tried so many
differents combinations of <choiceand <sequence>'s... and yet it
doesn't work :/
You will have to tell us about what it must be able to do, what you
tried, what you expected and what you got....
One solution is to approximate Items to contain anything at all. That
will work -- but is that good enough for your requirements?
All I got at best were errors saying "local complex type: The content
model is not determinist" -- because the element name "Item" is used in
both "types" of elements IIUC.
Maybe using more complex OO features of XML Schema would somehow help me
out?
NO. They will only make it worse ;)
An XML Schema must be written in such a way that it can be <<determined
immediately when parsing what branch of a content model to use>(my
formulation; The XML Schema spec has some formally correct but so
complicated that you can't read it ways to say that).
Example
<element name="goat">
<complexType>
<choice>
<sequence>
<element name="foo"/>
<element name="bar"/>
</sequence>
<sequence>
<element name="foo"/>
<element name="baz"/>
</sequence>
</choice>
</complexType>
</element>
--- when validating:
<goat>
<foo/>
<baz/>
</goat>
(which is valid), the schema must be constructed that the validator,
when it sees the <foo/element, does not have to _guess_ whether to
continue validating as
<sequence>
<element name="foo"/>
<element name="bar"/>
</sequence>
or
<sequence>
<element name="foo"/>
<element name="baz"/>
</sequence>
You have probably made that mistake ;)
A possible fix is:
<element name="goat">
<complexType>
<sequence>
<element name="foo"/>
<choice>
<element name="bar"/>
<element name="baz"/>
</choice>
</sequence>
</complexType>
</element>
Hope that helped.
Søren