Recently, we have a system whose input is XML. In order to validate those input file, we decided to validate it using XSD, which is the shorthand of XML schema definition and is used to regulate XML format.
I search the internet, but it seems that tutorials I found are not so complete, so I decided to write some problems I met to save future reader time.
I recommend you to read this tutorial first (which is very short and it may just cost half an hour), understanding the basic things about xsd, then continue here. You can also find a problems list in the next blog.
Basic Part
Element vs Attribute vs Text
<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
As above xml shows, person
, firstname
etc, the labels we use, are elements; gender
, which in key value form, is attribute; Anna
is the content/text of a element.
Define Complex Type
<xs:complexType name = "StudentType">
<xs:sequence>
<!-- content of this type -->
</xs:sequence>
<!-- attribute here -->
</xs:complexType>
Advance Part
Now, we will continue some advance topic of xsd.
Define Simple Type With Attribute
We already know that the differences between simple type and complex type:
- simple type can’t have element in it, only text; complex can have both
- simple type can’t have attribute; complex can.
So If we want the following element tag:
<time format="minutes">11:60</time>
How to achieve it?
<xsd:simpleType name="timeValueType">
<xsd:restriction base="xsd:token">
<xsd:pattern value="\d{2}:\d{2}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="timeType">
<xsd:simpleContent>
<xsd:extension base="timeValueType">
<xsd:attribute name="format">
<xsd:simpleType>
<xsd:restriction base="xsd:token">
<xsd:enumeration value="seconds"/>
<xsd:enumeration value="minutes"/>
<xsd:enumeration value="hours"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Difference Between xs:all
, xs:sequence
, xs:choice
When define complex type, we may use the above labels to organize our elements. So what is the differences of them? The following is the annotation of the three labels:
<all
id = ID
maxOccurs = 1 : 1
minOccurs = (0 | 1) : 1
{any attributes with non-schema namespace . . .}>
Content: (annotation?, element*)
</all>
<choice
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
{any attributes with non-schema namespace . . .}>
Content: (annotation?, (element | group | choice | sequence | any)*)
</choice>
<sequence
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
{any attributes with non-schema namespace . . .}>
Content: (annotation?, (element | group | choice | sequence | any)*)
</sequence>
From their annotation and definition, we can see following differences:
all | choice | sequence | |
---|---|---|---|
itself occur | [0, 1] | [0, unbounded] | [0, unbounded] |
inside element occur | [0, 1] | [0, unbounded] | [0, unbounded] |
content | element | element/group/choice/sequence | element/group/choice/sequence |
element order | no | no | must in same order with definition |
regex pattern (elements: A, B, C) | A*B*C*|A*C*B*|... |
A+|B+|C+ |
A+B+C+ |
Inheritance: extension
vs restriction
There are two ways of extend a type: extension
and restriction
.
restriction
can be used to restrict the father’s element and attribute (what kind of restriction? Including max, min, len, pattern etc). Types derived by restriction must repeat all the particle components, i.e. not adding new one or removing old one (actually, we can prohibit the use of some attribute, which works like remove one, see here for more examples).
<xs:simpleType name="possibility">
<xs:restriction base="xs:double">
<xs:maxInclusive value="1"/>
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
extension
, on the other hand, is used to extend original type.
More examples and problems can be found in my next tutorial.
Another Way to Reuse Elements – Group
Looking at the example, we understand how group used:
<xs:group name="subElements">
<xs:sequence>
<xs:element name="subElementA"/>
<xs:element name="subElementB"/>
</xs:sequence>
</xs:group>
And notice, we can only use choice
and sequnce
with group.
<xs:element name="ElementA">
<xs:complexType>
<xs:sequence>
<xs:element name="firstSubElement"/>
<xs:group ref="subElements"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ElementB">
<xs:complexType>
<xs:sequence>
<xs:element name="otherSubElement"/>
<xs:group ref="subElements"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Ref
- W3C tutorial
- tutorialspoint
- extend in xsd
- w3c all doc
- differences between all and sequence
- simple type with attribute
- restriction definition
- group vs sequence
Written with StackEdit.
评论
发表评论