跳至主要内容

XSD Tutorial (1): Startup

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

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (