xml schema maxOccurs = unbounded within xs:all

烈酒焚心 提交于 2019-12-29 09:12:26

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/14622677/xml-schema-maxoccurs-unbounded-within-xsall

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!