The new XML Schema system is about to become a W3C recommendation standard for overcoming the limitations of DTD (see the side bar,
The limitations of DTDs provide a rich syntax structure for XML documents. This article shows the flexibility of the pattern, indicating how to use the XML Schema system to define the most basic XML document construction block - element.
XML Schema is more powerful than DTD. To illustrate the powerful function of the XML Schema mechanism, the following three programs are briefly compared to different ways of representing elements. Listing 1 gives an XML document clip, and Listing 2 declares these two elements with DTD grammar, and Listing 3 is the corresponding XML Schema grammar form. Note that the same XML syntax used in Listing 3 is used. Through the mode, the verification parser can check if the elements invoceno are positive integers, whether the first character of the element ProductID is the letter between A to Z, behind the six Arabic numbers. Instead, the referenced DTD verification parser can only check if these elements are represented by a string.
Listing 1: XML document fragment
Listing 2: Describe the DTD fragment of the element in Listing 1
Listing 3: Describe the XML Schema in Listing 1
simpletype>
Using Namespaces in XML Schema In this collaboration world, a person may handle documents from multiple other groups, while different groups may wish to indicate their data elements in different ways. In addition, they may also reference the same name elements created by different groups in a document. How to distinguish between different definitions of the same name? XML Schema uses a namespace to distinguish these definitions.
Limitations of DTD Despite a mechanism to describe structured information, DTD successfully serves SGML and HTML developers for 20 years, but there is a serious limitations compared to XML Schema. DTD requirements elements consist of the following three components:
Text string text string with other child elements The mixed group of child DTD does not use XML syntax, with only limited support for type and namespace.
A given XML Schema defines a set of new names, such as element names, type names, attribute names, attribute group names, definitions and statements of these names are written in the mode. Listing 3 The name defined by InvoiceNo, ProductID, and ProductCode.
We say the name defined in the model belongs to its target namespace. The namespace itself has a fixed but unlimited name, must comply with the URL syntax. For example, for the mode segment in Listing 3, you can set the name of the namespace to: http://www.samplestore.com/account.
The namespace of the namespace is easy to confuse, although the URL does not point to a file containing the mode definition by http: //. In fact, this URL http://www.samplestore.com/account does not point to any file, just a assigned name. The definitions and declarations in the pattern may reference the names belonging to other namespaces. In this article, we call these namespaces for source namespaces. Each mode has a target namespace, but there may be multiple source namespaces. The names of the namespace may be quite long, but in the XML documentation, you can use yourself with the XMLNS declaration. To illustrate these concepts, we can add more content to the example mode in the list 4.
Listing 4: Target Name Space and Source Name Space
XMLns: xsd = 'http://www.w3.org/1999/xmlschema' XMLns: ACC = 'http://www.samplestore.com/account'> xsd: SimpleType> In the XML Schema in Listing 4, the name of TargetNameSpace is http://www.samplestore.com/account, which contains INVOICENO, PRODUCTID, and PRODUCTCE. These names belong to SchEMA, Element, SimpleType, Pattern, String, and Positive-Integer These names belong to the source namespace http://www.w3.org/1999/xmlschema, which is abbreviated as XSD via XMLNS declaration. Alias XSD does not have any special places, we can choose any other name. In the following part of this article, in order to facilitate and simplify, we use XSD representative namespace http://www.w3.org/1999/xmlschema, and omitted the qualifier XSD in some code snippet. In this example, TargetNameSpace occasionally acts as a source name space because the name ProductCode defines other names. Figure 1: Name Space in Listing 4 The mode clip in Listing 4 does not need to specify the location of the source mode file. For the entire "mode mode", http://www.w3.org/1999/xmlschema does not need to be specified, because its location is known. For the source namespace http://www.samplestore.com/account, you don't need to specify a location because it happens to be the target namespace defined in this file. In order to better understand how to specify the position of the mode and use the default namespace to see an example of the extension in Listing 5. Listing 5: Multiple source namespace, import a namespace XMLns = 'http://www.w3.org/1999/xmlschema' XMLns: ACC = 'http://www.samplestore.com/account' XMLns: part = 'http://www.partner.com/partscatalog'> SchemAlocation = 'http://www.productst4tandards.org/repository/alpha.xsd'/> simpletype> There is a namespace quotation in Listing 5: http://www.partner.com/partscatalog. This name is different from TargetNameSpace and standard namespace. Therefore, it is necessary to introduce the Import declaration element that indicates the file location containing the mode. The default namespace is http://www.w3.org/1999/xmlschema, and its XMLNS declaration has no name. Each non-qualified name such as Schema and Element belong to the default namespace http://www.w3.org/1999/xmlschema. If multiple names are referenced from a namespace, it is more convenient to specify it as the default name space. An XML instance document may reference the element names of multiple namespaces, which are defined in different modes. In order to reference and simplify the name of the namespace, the XMLNS declaration is also used. We use the XML Schema instance namespace to specify the location of the file. It should be noted that this property is different from the same name attribute of the XSD namespace in the previous example. Listing 6: Using multiple namespaces from multiple modes XML Version = "1.0"?> XMLns: part = 'http://www.partnerStore.com/partscatalog' XMLns: xsi = 'http://www.w3.org/1999/xmlschema-instance' XSI: schemAlocation = 'http://www.partner.com/partscataockttp://www.productstandards.org/repository/alpha.xsd http://www.sample.com/account http://www.samplestore.com/repository/schema1.xsd '> Figure 2: Name Space of Listing 5 and Listing 6 Defining element definition elements is the name and content model of the element. In XML Schema, the content model of the element is defined by its type, so the value of the instance elements in the XML document must match the type defined in the mode. Simple Type XML Schema specification defines a simple type of a value, as shown in Table 2: "Predefined Value Simple Type". Types include simple types and complex types. Simple type values cannot contain elements or properties. Complex types can produce the effect of nested elements in other elements, or add attributes for elements. (Above this example this article is a simple type, such as ProductCode. The XML Schema specification also includes a predefined simple type (see the simple type of the sidebar). The derived simple type constrained the value of the base type. For example, the value of derived simple type ProductCode is a subset of the base type string value. Simple, non-nesting elements are elements that do not contain attributes or other elements can be defined as simple types, whether predefined simple types or user-defined simple types, such as String, Integer, Decimal, Time, ProductCode, etc. Wait. Listing 7: Simple type of some elements Elements with properties must be complex types Now, try to increase attribute currency to the simple element Price to Listing 7. You can't do this, because the simple type of elements cannot have properties. If you want to increase attribute, you must define the Price element into complex types. In the example of Listing 8, we defined an anonymous type and did not explicitly name this complex type. In other words, the Name property of complex type complexType is not defined. Listing 8: A complex element type complextype> element> 45.50 price> -> Elements embedded in other elements must be a complex type in an XML document, and an element may embed other elements. This requirement can be represented directly in DTD. But XML Schema defines an element that has a type, and this type can contain a statement for other elements and properties. Table 1 shows a simple example. Table 1: Comparison of complex data types in DTD and XML Schema XML documentation Book> DTD Book> XML Schema Book> complextype> Although the XML code in Table 1 meets the DTD and XML Schema fragments, there is a big difference between the two. All elements in the DTD are global, and the XML Schema in the table allows Title and Author to be defined locally - only in the element BOOK. In order to achieve identical effects as DTD declarations in XML Schema, elementality Title and Author must be global, as shown in Listing 9. Element's ref attribute enables you to reference the elements of the previous declaration. Listing 9: Complex type defined with global simple type complextype> In the example shown in Table 1 and Listing 9, BookType is global and can be used to declare other elements. Instead, Listing 10 localically defines the type into the element BOOK and defines an anonymous element. It is to be noted that the XML document segments in Table 1 match the three mode clips in Table 1, Listing 9, and Listing 10. Listing 10: Hide BookType as a local type complextype> element> The complex constraint representation of the element provides greater flexibility than DTD for constraints representing the content model of the element. At the simplest level, like in DTD, you can associate attributes and element declarations, indicating that a given element set sequence: only 1 (1), 0 or multiple times (* ) Or once or more ( ). You can also represent additional constraints in XML Schema, which uses the Minoccurs and MaxocCurs properties of the Element element, as well as Choice, Group, and all elements. Listing 11: Representation of element types complextype> element> In Listing 11, the appearance of Title in the Book is optional (similar to DTD '"). However, Listing 11 also shows that there is at least one but not more than two authors in the BOOK element. The default value for the minoCCURS and MaxOcCurs properties is 1. Element Choice only allows one of its children to appear in an example. Another element ALL indicates such a constraint: all sub-elements in the group can occur at the same time, or they can appear in any order. Listing 12 shows that both Title and Author must appear (order) in the box, or do not appear. This constraint is difficult to represent in DTD. Listing 12: Point out that all types must be defined for the element xsd: all> xsd: complexType> xsd: element> More on the upper floor we have discussed the most basic concepts required to define elements in XML Schema, allowing you to appreciate its powerful features through some simple examples. There are also some more powerful mechanisms: XML Schema provides a wide range of support for type inheritance, allowing reusing the previously defined structure. With so-called Facets, you can derive new types, indicating a smaller subset of other types of types, such as by enumeration, range, or pattern matching to define subsets. In the examples of this article, the ProductCode type is defined using the pattern facet. Subtypes can also add more elements and attribute statements to the base type. There are several mechanism control whether or not to define subtypes, or whether it can replace the child type in a specific document. For example, it is possible to indicate the INVOICETYPE (Type of Invoice Number) does not allow sub-type, and no one cannot define the new version of InvoiceType. This constraint can also be expressed by specifying sub-types that cannot be used in specific contexts. In addition to subtypes, it can also define the equivalent type, so that a type of value can be replaced with another type. By declaring abstract elements or types, XML Schema provides a forced replacement mechanism. For your convenience, you can define and name the property group and element group, so that these groups can be reused later. XML Schema provides three elements --appinfo, documentation, and annotation - to make an annotation to facilitate the reader (Documentation) and AppInfo. Some attributes based on child elements can represent uniqueness constraints. XML Schema can be studied by W3C sites (see Resources), or visit the DW XML zone to learn more. Currently, the XML Schema specification has been approved and has become a candidate recommendation standard (Candidate Recommendation), without having to use it more and more. Reference By filling out a Web form, you can get an XSV (Alpha version of the University of Edinburgh / W3C, which is an XML Schema Validator. Download XML Schema Validator for Apache Projects for free. Extensibility's Business Software XML Schema Aware Editor / Validator Evaluation Edition. Download free tools from http://www.ibm.com/alphaworks. Background information for the latest XML Schema specification: XML Schema Part 0: Primer. The current W3C XML Schema specification document consists of two components: XML Schema Part 1: Structures and XML Schema Part 2: DataTypes.