Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

xsd - xml schema maxOccurs = unbounded within xs:all

Is it possible to have a combination of xs:all and xs:sequence?

I've have a xml structure with an element probenode which consist of the elements name, id, url, tags, priority, statuws_raw, active. And a combination of device and group.

device and group can occur zero or more times...

the solution below doesn't work because it is not allowed to use unbounded for an element. within an all group.

<xs:complexType name="probenodetype">
    <xs:all>
        <xs:element name="name" type="xs:string" />
        <xs:element name="id" type="xs:unsignedInt" />
        <xs:element name="url" type="xs:string" />
        <xs:element name="tags" />
        <xs:element name="priority" type="xs:unsignedInt" />
        <xs:element name="status_raw" type="xs:unsignedInt" />
        <xs:element name="active" type="xs:boolean" />
        <xs:element name="device" type="devicetype" minOccurs="0" maxOccurs="unbounded">
            <!-- zie devicetype -->
        </xs:element>
        <xs:element name="group" type="grouptype" minOccurs="0" maxOccurs="unbounded">
            <!-- zie grouptype -->
        </xs:element>
    </xs:all>
    <xs:attribute name="noaccess" type="xs:integer" use="optional" />
</xs:complexType>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In XSD 1.0, the children of xs:all must have maxOccurs set to 1.

In XSD 1.1 this constraint is lifted.

So your alternatives appear to be:

  • Use an XSD 1.1 processor (Saxon or Xerces-J).

  • Use XSD 1.0 and impose an order on the children of probenodetype. This is a problem if the order in which the children appear carries information (so id followed by url is different from url followed by id ...).

In some simple cases it's feasible to write a content model that accepts precisely what you suggest you want, using only choice and sequence, but with seven required elements the resulting content model is likely to be too long and complex to be useful.

At this point some users give up and write a complex type with a repeatable OR-group and move the responsibility for checking that name, id, url, etc. all occur at least once and at most once into the application; that allows the generator of the XML not to have to worry about a fixed order (and opens a side channel for information leakage, which matters to some people) but also renders the schema somewhat less useful as documentation of the contract between data provider and data consumer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...