xml schema meaning question
We have a generated XML schema that I am a bit confused on the meaning of the schema (ie, what exactly it is looking for to be valid)
<xs:element name="element">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="firstitem_id" type="xs:integer" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="seconditem_id">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="order" type="xs:integer" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required" />
</xs:complexType>
</xs:element>
From what I was told, this is really asking for the following:
<element>
<firstitem_id>17</firstitem_id>
<seconditem_id order="2">34</seconditem_id>
</element>
Is this true, and why wo开发者_StackOverflow中文版uld this specification show up in this manner?
According to your schema, provided XML isn't valid, because element element
requires attribute id
to be set.
Another way to represent you XML is using references in XSD, e.g. equivalent schema:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="element">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="firstitem_id" type="xs:integer" />
<xs:element ref="seconditem_id" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="seconditem_id">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="order" type="xs:integer" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Sample valid XML:
<element id="12"/>
or
<element id="12">
<firstitem_id>17</firstitem_id>
<seconditem_id>10</seconditem_id>
<seconditem_id order="2">34</seconditem_id>
<seconditem_id>20</seconditem_id>
<seconditem_id>30</seconditem_id>
</element>
That schema fragment defines the following structural rules for the "element" element:
- It's name is "element"
- It must have an "id" attribute with an integer value (your example does not)
- The child element "firstitem_id"
- is optional
- can only be used once
- it's value must be an integer
- and it must appear before "seconditem_id" if both elements are in the instance doc
- The child element "seconditem_id"
- is also optional
- can appear more than once
- it's value must be an integer
This is one valid example:
<element id="1">
<firstitem_id>17</firstitem_id>
<seconditem_id order="2">34</seconditem_id>
</element>
This is another:
<element id="2">
<seconditem_id order="2">34</seconditem_id>
</element>
This is another:
<element id="3">
<firstitem_id>17</firstitem_id>
</element>
And another:
<element id="4">
<firstitem_id>17</firstitem_id>
<seconditem_id order="2">34</seconditem_id>
<seconditem_id order="3">35</seconditem_id>
</element>
And another:
<element id="5">
<seconditem_id order="2">34</seconditem_id>
<seconditem_id order="3">35</seconditem_id>
</element>
精彩评论