Author: Tuesday, April
You should now know how XML only shows the structure of the data and does not care about how the data is specifically described, whether the data is correct. The mandatory structured demand for the XML document is implemented by DTD (document type description). That is the topic previously described in this series of articles. In this article, we summarize the shortcomings of DTD, which is a more powerful standard XML Schema.
DTD's trouble
Using DTD Although the element, the desired element and how to organize the elements in a given XML document, it will be troublesome when you want to apply a data type for a particular element. The DTD specification strictly defines the structure, but only the content type specification of the relative function is supported, but the mandatory structured is not aware, such as how Date is specified, it must contain effective values?
This is to count XML Schema, XML Schema is currently submitted to W3C in 2001, which means that it will eventually become general purpose recommendations. If you are interested in this, you may wish to see the materials such as the W3C website to find some official documents and content. Note that other Schema definitions are also available, including Japanese standard Relax and Microsoft's XDR. However, XML Schema is the only recommended standard for W3C, so I only explain it in this article.
XML Schema not only allows you to define the structure of the XML document and allow you to constrain the contents of the document, which is different from DTD. In addition, an XML Schema itself is an XML document, and its label-based syntax is more clear than the special characters in the DTD.
SCHEMA Overview
XML Schema is created with a predetermined XML element and attribute, which define the structure and content mode of the document. The corresponding set of delicate rules (these rules are very interesting to represent DTDs) specify the legal use of each Schema element or attribute. If these rule parsers will refuse to parse your schema and any document that is associated with it.
List a Listing A - Catalog Schema
XML Version = "1.0" encoding = "UTF-8"?>
Now let's take a look at the XML Schema example displayed in List A, which is described in the list of book catalogs we used in List B. Listing B has a small change: Catalog as the root element has two new properties, which are associated with Catalog Schema in Listing A.
You look at Catalog Schema, soon you will notice that it contains the standard XML head XML Version = "1.0"?>, This means that schema yourself is an XML document. And any Schema's root element must be Schema, which has one or more illustrates its own properties. In this case, Schema's NameSpace Define Properties (XMLns) define namespaces to XS, which will be used as root namespace of all elements in the document. -------------------------------------------------- ------------------------------
What is the namespace XML defines the namespace to reuse after packaging XML elements. In order to use the elements defined in the namespace of the XML document, you must declare the namespace through the XMLNs property. You must also define a shortcut prefix for this name space as the root element in your document, so that the namespace can be available in the document. The prefix is an identifier of any element of any imported namespace for the document. This process is like giving the library icons in Visual Basic or imported modules; the namespace under C , Java or .NET also have the same meaning.
-------------------------------------------------- ------------------------------
The elements of our Schema example are Annotation, which represents documents related to their parent elements. Annotation can contain one of two sub-elements, or documentation or appinfo, or both. The former is used for readable documents, while the latter is used to save instructions for supplying program processing.
Next we defined two major elements (root elements Catalog and their sub-elements Book), they used two ELEMENT elements in their own documents. These elements contain the properties of the definition name and the content permitted by each element. In this case, the Catalog element is defined as a CatalogType type, while the BOOK element is defined as the ElementType type; these two types will be defined in the Schema document later.
What is type?
I have already said that XML Schema allows you to declare the elements in the XML document as a specific type, allowing the parser to check the contents of the document and its structure. XML Schem defines two main data types: predefined simple types and complex types. The main difference between these two data types is that complex types can contain other elements like data, and only data can only be included. The simple type gives XML Schema low-level type inspection capabilities, allowing you to define elements as one of the types A in Figure A.
Simple Type Defines String String Data. Boolean binary type TRUE or FALSE. Date calendar date, format is CCYY-MM-DD. DateTime calendar date and time. Time24 hours of format can be adjusted according to time zone. Decimal's decimal number of arbitrary precision and bits. Integer integer Float standard 32-bit floating point number.
Figure A XML Schema predefined simple type
You can also define your own simple type. For more deep understanding of the type of XML Schema data type, please see "XML Schema Part 2: DataTypes" on the W3C website.
Complex types are defined by the ComplexType element, which typically has at least one Name property, with index types when declaring other elements, unless it is within a certain element (see the next section). All complex types contain a content definition type, which is the main function to define the content mode that can be included. See Figure B for some available content modes.
Complex Type Definition Sequence All elements within its definition must appear in order, and the range is specified by MinoCCURS and MaxocCurs. There is only one element in the range of CHOICE. Any of any elements defined by Any must appear. SimpleContent This complicated type contains only non-nest elements. The previously defined simple type can be expanded by including extended elements. ComplexContent This complicated type can only contain other elements. The previously defined complex type can be expanded by including extended elements. Attribute This complex type can only contain naming properties. Figure B Some permitted XML Schema complex types
Our first complexType element in Schema defines the BookType type, you can see from the document comment element, which gives a single book construction model in the directory. BookType contains a sequence element that tells the parser This complicated type element must contain all the elements that appear in the Sequence tag in the same accurate order. For BookType, Elements Author, Title, Genre, Price and Publish_Date must appear within the booktype element.
What is DESCRIPTION? It appears within the sequence element, is there any necessary? of course not. The Description element has a minoCCurs property that defines the minimum number of elements that can occur in complex types. In this case, the value of Minoccurs is zero, so Description is an optional element.
This is also true of Author elements. It has a maxoccurs property, but there is no value, meaning that Author elements can appear unlimited times in Sequence, obviously, there is at least one of a book author, but not necessarily one. There is no element that doesn't have a Maxoccurs property must and can only appear once in Sequence, so all other elements in BookType Sequence are necessary and can only appear once.
In our example, the second defined in Catalog Schema is also the last complex type is a CatalogType complex type. It also contains more than one Sequence, you can see this from the boundless maxoccurs property.
Interpretation
Look at your background knowledge, this article uses the example schema's structure. If you don't here, you will not only call you, the Book and Catalog elements do not use a formal complex type declaration to define Catalog Schema is also possible. This is the case. Note that the ComplexType element in the list C is nested within the ELEMENT element, and the child element of Catalog's sequence element has a REF property that tells the parser with a reference to the previously defined BOOK element.
I have heard a lot of people like this: "Very good, if this is the case, then do you have to use these Fishermen's way?" It is very simple, we will go to illustrate the important point of XML Schema: it It is scalable. By defining the type of way you can reuse them in multiple documents, you can also extend them with different Schema, which is like reuse or extending abstract data types when developing applications.
tool
So far, you should realize that the syntax of XML Schema is not simple. Although Schema can be created by simple text editor, it may be called you to vomit blood. In order to better utilize it, you can use several XML tools, and some of the recent tools even provide a graphical interface that creates XML Schema. XML SPY and CAPE CLEAR Studio are all full-scale XML integrated development environments with XML Schema creation feature. DTD2XS is a DTD-TO-XML SCHEMA conversion tool that has two forms: an application and Java class running separately. Like DTD, standard XML Schema defines a lot, you can use it according to your needs in the development of applications. in conclusion
XML Schema has the ability to force document content and structure, which is a new standard in the XML world not only important and powerful. In this article, I only explained the basic principles in this article, but I hope these elaborates will give you a basis for continued progress.