If there is no such OO shadow in the current language, I am embarrassed to call it language. In terms of XML semantic constraints, DTD is simple, but not powerful enough. It is completely straightforward to describe, so it has replaced dtd xsd (XML Schema Definition ), XSD introduces data types, provides various mechanisms for customizing data types, and even finds inheritance, polymorphism, and other OO features. However, learning is relatively complicated, start learning XSD from this note.
1. The XSD file is an XML file.
An XSD file is an XML file that meets all the requirements of the XML file format. For example, there is only one root element (this root element is <schema/> ), all technologies that are effective for XML files are equally effective for XSD files, including the use of XSD files as semantic constraints.
2. root element of the XSD File
The root element of the XSD file is <schema/>, which can also have its own attributes. Let's look at the example of spring-beans-3.2.xsd first:
I don't know if the person who saw this explanation is confused for the first time. I am confused when I first saw it in w3c textbooks, but it doesn't matter. Our main purpose is to apply it, later, you can write the xsd file like spring.
The attributes of the root element schema are listed as follows (mixed first ):
Attribute |
Description |
Value |
Id |
Unique ID that identifies the element |
|
AttributeFormDefault |
|
Qualified: The namespace prefix must be used. Unqualified: (default) The namespace prefix is not required. |
ElementFormDefault |
Specifies whether to use namespace restrictions when the XML document uses the partial elements defined in the schema. |
The value and meaning are the same as those of attributeFormDefault. |
BlockDefault |
Set the default value of the block attribute on element and complexType in schema. Block attribute is used to prevent replacement of the original type with the specified derived type |
# Free combination of all or extension, restriction, and substitution For example, extension indicates to prevent the extension-derived complex type from replacing the complex type. |
FinalDefault |
Set the final default values for element, simpleType, and complexType in schema. The final attribute is used to prevent the specified derived type from deriving a new type. |
For element and complexType: The value can be # all or a free combination of extension and restriction For simpleType: The value can be a free combination of # all or restriction, list, and union. |
TargetNamespace |
Sets the URI reference of the schema namespace. |
|
Version |
Set schema version |
|
Xmlns |
Sets the URI reference of one or more namespaces used by the schema. |
|
Any attributes |
Set any other attributes with the non-schema namespace |
|
3. Use the xsd file in the xml document
Based on whether the reference xsd file defines the targetNamespace attribute, You can import the corresponding xsd file in two cases:
(1) introduce an XSD without a namespace
(2) Introduce XSD with namespace
The XSD file introduces the XMLSchema of w3, while the XML file generally introduces the XMLSchema-instance.
4. Notes for XSD files
As an XML file, the XSD file can also use the annotation format. In addition, the XSD file can also be annotated using XML elements, which is the <annotation> element, <annotation> elements work by using two sub-elements: <document> and <appinfo>. The <document> element mainly contains comments suitable for human reading, however, <appinfo> mainly stores comments for other applications. Spring XSD files are widely used.
Format comment, here is not an example.
5. Import other XSD files into the XSD File
In the XSD file, if you think it is necessary, you can also import other XSD files, which is modular in OO and encapsulate the related definitions in an XSD file, and then import it to other XSD files. There are three import methods:
Import Method |
Prerequisites |
Attribute |
Include |
The imported XSD file either has no namespace or has the same namespace as the current file. |
Id: Specifies the unique identifier of the <include> element; schemaLocation: Specifies the URI of the imported XSD File |
Redefine |
The premise is the same as include. The difference is that redefine can accept <simpleType> or <complexType> to redefine the content in the imported XSD. |
Same as include element attributes |
Import |
The imported XSD file either has no namespace or the namespace is different from the current file. Therefore, the imported XSD file retains the original namespace. |
In addition to id and schemaLocation, there is also a namespace attribute used to specify the namespace of the imported XSD File |
Similar to # include in C, the element imported into the XSD file can only be used as a child element of the root element <schema>, and is located before all elements except the comment element <annotation>, the order of the three import methods is not required.
6. XSD Elements
In XSD, the most powerful one is to provide support for data types and customize data types. In this document, I do not want to enter the quarary OF THE XSD data type, the elements in the XSD file are familiar. These elements are equivalent to keywords in programming languages. XSD defines the XML structure with these elements, you can also directly access the relevant part of the W3C online tutorial: XML Schema reference manual.