[XML Schema] Error "content model is not determinist"

Discussion in 'XML' started by Damiano ALBANI, Aug 21, 2006.

  1. Hello,

    I'd like to write a single XML Schema for both these documents :

    <Items>
    <Item xml:id="id1">
    <Name>ABC</Name>
    ...
    </Item>
    <Item xml:id="id2">
    <Name>DEF</Name>
    ...
    </Item>
    ...
    </Items>

    and

    <Items>
    <Item ref="id1"/>
    <Item ref="id2"/>
    ...
    </Items>

    That looks "easy", but I've been googling for hours and tried so many
    differents combinations of <choice> and <sequence>'s... and yet it
    doesn't work :/
    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?

    Cheers,

    --
    Damiano ALBANI
    Damiano ALBANI, Aug 21, 2006
    #1
    1. Advertising

  2. Damiano ALBANI

    Soren Kuula Guest

    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 <choice> and <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
    Soren Kuula, Aug 21, 2006
    #2
    1. Advertising

  3. Thanks for the tips Soren !

    Here's what I wrote, which works quite well -- not perfectly, but the
    best I could thought of :

    <xsd:group name="Item_Content">
    <xsd:sequence>
    <xsd:element name="Name"
    type="xsd:string"/>
    <xsd:element name="Type"
    type="xsd:string"/>
    ...
    </xsd:sequence>
    </xsd:group>


    <xsd:complexType name="Item_Type">
    <xsd:group ref="Item_Content"
    minOccurs="0"
    maxOccurs="1"/>
    <xsd:attribute name="id"
    type="xsd:ID"/>
    <xsd:attribute name="ref"
    type="xsd:IDREF"/>
    </xsd:complexType>

    So, the only problem is that an Item element with both (content + @id)
    _and_ @ref attribute is considered valid.

    In my real-world application, I don't manipulate simply Item elements
    but there's several complex element types. So that means I need to write
    many "dual" element definitions (content through <xsd:group> + actual
    type referring to that group). As I'm no expert in XML Schema, would
    there be a way to avoid this redundancy ?

    Cheers,

    --
    Damiano ALBANI
    Damiano ALBANI, Aug 22, 2006
    #3
  4. Damiano ALBANI

    Soren Kuula Guest

    Damiano ALBANI wrote:
    > Thanks for the tips Soren !


    Happy to be of help
    > So, the only problem is that an Item element with both (content + @id)
    > _and_ @ref attribute is considered valid.


    Can't do anything about that. Attribute inter-dependencies are not
    supported in XSD.

    > In my real-world application, I don't manipulate simply Item elements
    > but there's several complex element types. So that means I need to write
    > many "dual" element definitions (content through <xsd:group> + actual
    > type referring to that group). As I'm no expert in XML Schema, would
    > there be a way to avoid this redundancy ?


    Sorry, don't understand that. Could you post an example?

    Søren
    Soren Kuula, Aug 23, 2006
    #4
  5. Soren Kuula wrote:

    >> So, the only problem is that an Item element with both (content + @id)
    >> _and_ @ref attribute is considered valid.

    >
    > Can't do anything about that. Attribute inter-dependencies are not
    > supported in XSD.


    Okay.

    >> In my real-world application, I don't manipulate simply Item elements
    >> but there's several complex element types. So that means I need to
    >> write many "dual" element definitions (content through <xsd:group> +
    >> actual type referring to that group). As I'm no expert in XML Schema,
    >> would there be a way to avoid this redundancy ?

    >
    > Sorry, don't understand that. Could you post an example?


    I was referring to the example in my post. For each element type, I need
    to define :

    * a <xsd:group>
    * a <xsd:complexType>, using the previous <xsd:group>

    So I was thinking of an XML Schema construct to avoid "leaking"
    <xsd:group> definitions outside <xsd:complexType> where they are used.
    No big deal, but that would make the final schema a bit "cleaner".

    Cheers,

    --
    Damiano ALBANI
    Damiano ALBANI, Aug 25, 2006
    #5
  6. Damiano ALBANI

    Soren Kuula Guest

    Damiano ALBANI wrote:
    > Soren Kuula wrote:
    >
    >>> So, the only problem is that an Item element with both (content +
    >>> @id) _and_ @ref attribute is considered valid.

    >>
    >>
    >> Can't do anything about that. Attribute inter-dependencies are not
    >> supported in XSD.

    >
    >
    > Okay.
    >
    >>> In my real-world application, I don't manipulate simply Item elements
    >>> but there's several complex element types. So that means I need to
    >>> write many "dual" element definitions (content through <xsd:group> +
    >>> actual type referring to that group). As I'm no expert in XML Schema,
    >>> would there be a way to avoid this redundancy ?

    >>
    >>
    >> Sorry, don't understand that. Could you post an example?

    >
    >
    > I was referring to the example in my post. For each element type, I need
    > to define :
    >
    > * a <xsd:group>
    > * a <xsd:complexType>, using the previous <xsd:group>
    >
    > So I was thinking of an XML Schema construct to avoid "leaking"
    > <xsd:group> definitions outside <xsd:complexType> where they are used.
    > No big deal, but that would make the final schema a bit "cleaner".
    >

    Well you don't *have* to use a group when making a complex type. I think
    you can always replace a group reference by the content of the referred
    group. It's just a sort of macro mechanism.

    Søren
    Soren Kuula, Aug 27, 2006
    #6
    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. Markus
    Replies:
    1
    Views:
    1,478
    Markus
    Nov 23, 2005
  2. Fred Smith
    Replies:
    1
    Views:
    918
    Martin Honnen
    Feb 5, 2004
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,158
    Stanimir Stamenkov
    Apr 25, 2005
  4. Michael Strorm
    Replies:
    10
    Views:
    4,817
    Peter Flynn
    Aug 21, 2005
  5. Replies:
    3
    Views:
    2,764
Loading...

Share This Page