The <any> element enables us to extend the XML document with elements not specified by the schema!
<any>元素可以使我们在XML文档中添加没有被schema 定义过的新元素从而扩充XML文档。
The <any> Element
<any>元素
The <any> element enables us to extend the XML document with elements not specified by the schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <any> element we can extend (after <lastname>) the content of "person" with any element:
下面的例子是名为"family.xsd"的一份XML schema片段。它展示了"person"元素的声明。用上<any>元素,我们可以在"person"元素的内容里扩充任意元素(在<lastname>的后面)
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Now we want to extend the "person" element with a "children" element. In this case we can do so,even if the author of the schema above never declared any "children" element.
现在我们想在"person"元素中添加"children"元素,即使这篇schema的作者从未声明过什么"children"元素,我们也可以做到。
Look at this schema file,called "children.xsd":
请看下面名为"children.xsd"的schema文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="elementFormDefault="qualified">
<xs:element name="children">
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
</xs:schema>
The XML file below (called "Myfamily.xml"),uses components from two different schemas; "family.xsd" and "children.xsd":
下面的XML文件(叫做"Myfamily.xml"),用上了来自"family.xsd" 和"children.xsd"两篇不同schema的组件
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.comfamily.xsd
http://www.w3schools.comchildren.xsd">
<person>
<firstname>Hege</firstname>
<lastname>Refsnes</lastname>
<children>
<childname>Cecilie</childname>
</children>
</person>
<firstname>Stale</firstname>
</persons>
The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element.
上述XML文件是有效的,因为"family.xsd" schema允许我们在"person"元素里的"lastname"元素后面扩充一个任意元素。
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.
<any> 和<anyAttribute>元素是用于制造可扩展文档的!它们允许文档含有没有在主要XML schema里声明过的其它新元素。
XSD <anyAttribute> 元素
w3pop.com / 2006-09-21
XSD <any> 元素 XSD 元素替代
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema!
<anyAttribute>元素可使我们在XML文档中添加未被schema指定过的属性
The <anyAttribute> Element
<anyAttribute>元素
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <anyAttribute> element we can add any number of attributes to the "person" element:
下面是名为"family.xsd"的XML schema片段。它显示了"person"元素的声明。通过使用<anyAttribute>元素我们可以给"person"元素添加任意数量的属性。
<xs:anyAttribute/>
Now we want to extend the "person" element with a "gender" attribute. In this case we can do so,even if the author of the schema above never declared any "gender" attribute.
现在我们想在"person"元素中添加"gender"属性,即使这篇schema的作者从未声明过什么"gender"属性,我们也可以做到。
请看下面名为"attribute.xsd"的schema文件:
<xs:attribute name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
下面的XML文件(叫做"Myfamily.xml"),用上了来自"family.xsd" 和"attribute.xsd"两篇不同的schema组件。
http://www.w3schools.comattribute.xsd">
<person gender="female">
<person gender="male">
The XML file above is valid because the schema "family.xsd" allows us to add an attribute to the "person" element.
上述XML文件是有效的,因为"family.xsd" schema允许我们在"person"元素里添加属性
<any> 和<anyAttribute>元素是用于扩展文档的!它们允许文档含有没有在主要XML schema里声明过的其它新元素。
XSD 元素替代
XSD <anyAttribute> 元素 XSD 实例
With XML Schemas,one element can substitute another element.
用XML Schema,一个元素可替代另一元素。
Element Substitution
元素替代(Element Substitution)
Let's say that we have users from two different countries: England and Norway. We would like the ability to let the user choose whether he or she would like to use the Norwegian element names or the English element names in the XML document.
假设有两个分别来自英国和挪威的使用者,我们很希望有能力能让他或她进行选择,在XML文档里的元素名称中选择他们所擅长的语言,是英文呢?还是挪威文呢?
To solve this problem,we could define a substitutionGroup in the XML schema. First,we declare a head element and then we declare the other elements which state that they are substitutable for the head element.
为解决这个问题,我们在XML schema里定义了替代组。首先,我们声明了一个标题元素,接着,我们声明已说明可替代标题元素的其他元素
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
In the example above,the "name" element is the head element and the "navn" element is substitutable for "name".
在上面的例子里,"name"元素是标题元素,"navn"元素可以替代"name"。
Look at this fragment of an XML schema:
看下面的XML schema片段:
<xs:complexType name="custinfo">
<xs:element ref="name"/>
</xs:complexType>
<xs:element name="customer" type="custinfo"/>
<xs:element name="kunde" substitutionGroup="customer"/>
A valid XML document (according to the schema above) could look like this:
一份有效 的XML文档(根据上述schema的XML文档)应该像这样:
<customer>
<name>John Smith</name>
</customer>
or like this:
或者像这样:
<kunde>
<navn>John Smith</navn>
</kunde>
Blocking Element Substitution
关闭元素替代(Element Substitution)
To prevent other elements from substituting with a specified element,use the block attribute:
为了防止其他元素被已指定的元素替代(Element Substitution),可以用block属性:
<xs:element name="name" type="xs:string" block="substitution"/>
看这段XML schema片段:
<xs:element name="customer" type="custinfo" block="substitution"/>
A valid XML document (according to the schema above) looks like this:
一份有效的XML文档(根据上述的schema的XML文档)应该像这样:
BUT THIS IS NO LONGER VALID:
但是这样就不再有效了
Using substitutionGroup
使用替代组(substitutionGroup)
The type of the substitutable elements must be the same as,or derived from,the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element.
可替代元素类型应和标题元素的类型相同,或是从中派生出来的。如果可替代元素类型和标题元素的类型相同,你就不需要再指明可替代元素的类型了
Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements,otherwise it will not work!
注意在可替代元素组里的所有元素(标题元素和可替代元素)必须声明为“全域元素(global element)”,否则它是不会作用的!
What are Global Elements?
什么是“全域元素”?
Global elements are elements that are immediate children of the "schema" element! Local elements are elements nested within other elements.
“全域元素”是"schema"元素下面的直接子元素。“本地元素”是嵌套在别的元素里的元素。
XSD 实例
XSD 元素替代 XSD 字符串数据类型
This chapter will demonstrate how to write an XML Schema. You will also learn that a schema can be written in different ways.
这章将示范如何写一份XML Schema.你也会了解到可以使用不同的方法书写schema。
An XML Document
一份XML文档
Let's have a look at this XML document called "shiporder.xml":
让我们看这份名为"shiporder.xml"的XML文档
<shiporder orderid="889923"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<title>Hide your heart</title>
<price>9.90</price>
</shiporder>
The XML document above consists of a root element,"shiporder",that contains a required attribute called "orderid". The "shiporder" element contains three different child elements: "orderperson","shipto" and "item". The "item" element appears twice,and it contains a "title",an optional "note" element,a "quantity",and a "price" element.
上述XML 文档有根目录元素"shiporder",它有个必需的属性叫做"orderid","shiporder"元素包含了三个不同的子元素: "orderperson","shipto" 和 "item"."item"元素出现了两次,它包含了一个"title"元素,一个任意的"note"元素,一个"quantity"元素和一个 "price"元素。
The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against a schema. The line:
xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder.xml").
上面的一行xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",告诉了XML解析器这个文档应该被一份schema检验。xsi:noNamespaceSchemaLocation= "shiporder.xsd"指定了schema应该所在位置(这里它以"shiporder.xml"文件待在相同的文件夹里)。
Create an XML Schema
创建一篇XML Schema
Now we want to create a schema for the XML document above.
现在我们想要为上面的XML文档创建一份schema
We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow the structure in the XML document and define each element as we find it. We will start with the standard XML declaration followed by the xs:schema element that defines a schema:
开始先打开叫做"shiporder.xsd"的新文件。为创建新的schema,我们可以简单地按照XML文档的结构,每当发现一个元素时就进行定义。一开始先作标准的XML声明,接下来是定义了schema的xs:schema元素:
<?xml version="1.0" encoding="ISO-8859-1" ?>
http://www.w3.org/2001/XMLSchema">
...
In the schema above we use the standard namespace (xs),and the URI associated with this namespace is the Schema language definition,which has the standard value ofhttp://www.w3.org/2001/XMLSchema.
在上述schema里我们用了标准的名称空间(xs),并且和这个名称空间相联系的URI是Schema语言定义,它有个标准值Next,we have to define the "shiporder" element. This element has an attribute and it contains other elements,therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a xs:sequence element that defines an ordered sequence of sub elements:
接下来,我们必须定义"shiporder”元素。这个元素有个属性,并包含着其他的元素,因此我们把它看成是复合类型。"shiporder"元素的子元素由xs:sequence元素包围着,xs:sequence元素定义了子元素一定顺序的排列。
<xs:element name="shiporder">
<xs:complexType>
...
Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type (xs:string) is prefixed with the namespace prefix associated with XML Schema that indicates a predefined schema data type:
那么我们必须将"orderperson"元件定义为简单类型(因为它不含有任何属性或其它元素)。种类用名称空间(namespace)的前缀使用与XML Schema相关的名称空间前缀化,这里的XML Schema指明了一个前缀的Schema数据类型。
<xs:element name="orderperson" type="xs:string"/>
接下来,我们必须定义两个复合类型的元素,"shipto" 和 "item"。先由定义"shipto"元素开始:
<xs:element name="shipto">
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
With schemas we can define the number of possible occurrences for an element with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and minOccurs specifies the minimum number of occurrences for an element. The default value for both maxOccurs and minOccurs is 1!
用schema,我们可以用maxOccurs(最多出现次数)和minOccurs(最少出现次数)属性来定义一个元素的可能出现次数。maxOccurs指定了一个元素的最多出现次数,minOccurs指定了一个元素的最少出现次数。maxOccurs
和minOccurs的默认值都是1。
Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element. This is specified by setting the maxOccurs attribute of the "item" element to "unbounded" which means that there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element is optional. We have specified this by setting the minOccurs attribute to zero:
现在我们可以来定义"item"元素,这个元素可以在"shiporder"元素里重复出现。这可以通过设置"item"元素的maxOccurs属性为 "unbounded"实现,属性为"unbounded"意味着"item"元素可以根据编者意愿重复出现多次。要注意"note"元素是任意的,我们可以通过设置minOccurs属性为0来实现。
<xs:element name="item" maxOccurs="unbounded">
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
We can now declare the attribute of the "shiporder" element. Since this is a required attribute we specify use="required".
我们现在可以声明"shiporder"元素的属性了。因为这是项必须属性,我们可以指定:use="required"。
Note: The attribute declarations must always come last:
注意:属性声明必须总是放在最后
<xs:attribute name="orderid" type="xs:string" use="required"/>
Here is the complete listing of the schema file called "shiporder.xsd":
下面是"shiporder.xsd" schema文件的完整例子:
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:attribute name="orderid" type="xs:string" use="required"/>
Divide the Schema
划分Schema
The prevIoUs design method is very simple,but can be difficult to read and maintain when documents are complex.
上面的设计方法非常简单,但因为文件是复合(或复杂的,Complex)的,所以难于阅读和利用。
The next design method is based on defining all elements and attributes first,and then referring to them using the ref attribute.
下面的设计方法是:先定义所有的元素和属性,然后用ref属性引用它们。
Here is the new design of the schema file ("shiporder.xsd"):
下面是schema文件的新设计方式("shiporder.xsd"):
<!-- definition of simple elements -->
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- definition of attributes -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- definition of complex elements -->
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
<xs:element name="item">
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
<xs:attribute ref="orderid" use="required"/>
Using Named Types
使用有名称的类型
The third design method defines classes or types,that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements,and then point to them through the type attribute of the element.
第三种设计方法定义了种类或类型,这使我们能重新用元素定义。通过给简单类型和复合类型元素命名,接着在元素的种类属性类型里指明它们的方法来做到这点。
Here is the third design of the schema file ("shiporder.xsd"):
这是schema文件("shiporder.xsd")的第三份构思
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="inttype">
<xs:restriction base="xs:positiveInteger"/>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal"/>
<xs:simpleType name="orderidtype">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
<xs:complexType name="shiptotype">
<xs:sequence>
<xs:element name="name" type="stringtype"/>
<xs:element name="address" type="stringtype"/>
<xs:element name="city" type="stringtype"/>
<xs:element name="country" type="stringtype"/>
</xs:sequence>
<xs:complexType name="itemtype">
<xs:element name="title" type="stringtype"/>
<xs:element name="note" type="stringtype" minOccurs="0"/>
<xs:element name="quantity" type="inttype"/>
<xs:element name="price" type="dectype"/>
<xs:complexType name="shipordertype">
<xs:element name="orderperson" type="stringtype"/>
<xs:element name="shipto" type="shiptotype"/>
<xs:element name="item" maxOccurs="unbounded" type="itemtype"/>
<xs:attribute name="orderid" type="orderidtype" use="required"/>
<xs:element name="shiporder" type="shipordertype"/>
The restriction element indicates that the datatype is derived from a W3C XML Schema namespace datatype. So,the following fragment means that the value of the element or attribute must be a string value:
约束元素指出了这个数据类型是由一个W3C XML Schema名称空间数据类型派生出来的。所以,下面的片段意味着元素或属性的值必须是一个字符串的值
The restriction element is more often used to apply restrictions to elements. Look at the following lines from the schema above:
约束元素常常用于给元素添加约束。请看上述schema中的几行:
This indicates that the value of the element or attribute must be a string,it must be exactly six characters in a row,and those characters must be a number from 0 to 9.
这指出了元素或属性的值必须是字符串,而且必须是一排6个从0到9之间的数字。
XSD 字符串数据类型
XSD 实例 XSD 日期数据类型
String data types are used for values that contains character strings.
字符串数据类型用于定义字符串的值的。
String Data Type
字符串数据类型
The string data type can contain characters,line Feeds,carriage returns,and tab characters.
字符串数据类型包括字符,换行符,回车符,和制表符
The following is an example of a string declaration in a schema:
下面是关于字符在schema(XML公式)里的声明方法的例子:
<xs:element name="customer" type="xs:string"/>
An element in your document might look like this:
你文档中也许有这样的元素:
<customer>John Smith</customer>
Or it might look like this:
它也许会是这样:
<customer> John Smith </customer>
Note: The XML processor will not modify the value if you use the string data type.
注意:如果你用字符串数据类型,XML处理器将不会修改数值。
NormalizedString Data Type
规格化的字符串数据类型(NormalizedString Data Type)
The normalizedString data type is derived from the String data type.
规格化的字符串数据类型是从字符数据类型里派生出来的。
The normalizedString data type also contains characters,but the XML processor will remove line Feeds,51)">规格化的字符串数据类型也包括字符,但XML processor会清除换行符,回车符和制表符
The following is an example of a normalizedString declaration in a schema:
下面的是关于一个规格化的字符串数据类型(normalizedString)在Schema中声明方法的例子:
<xs:element name="customer" type="xs:normalizedString"/>
你的文档中也许有像这样的元素:
或者它也许会是这样:
Note: In the example above the XML processor will replace the tabs with spaces.
注意:上述例子里XML处理器会用空格符替代制表符
Token Data Type
记号(token)数据类型
The token data type is also derived from the String data type.
记号数据类型也是从字符串数据类型里派生出来的
The token data type also contains characters,tabs,leading and trailing spaces,and multiple spaces.
记号数据类型也包括字符,但XML处理器会清除换行符,回车符,制表符,头尾的空格,以及对于的空格。
The following is an example of a token declaration in a schema:
下面是关于符号(token)在schema中声明的例子:
<xs:element name="customer" type="xs:token"/>
你文档中的元素也许像这样:
或者它也有可能像这样:
Note: In the example above the XML processor will remove the tabs.
注意:在上述例子里,XML处理器会清除制表符
String Data Types
Note that all of the data types below derive from the String data type (except for string itself)!
要注意下面所有的数据类型都是从字符串数据类型里派生出来的(除了字符串本身)
Name
名称 Description
解释
ENTITIES
ENTITY
ID A string that represents the ID attribute in XML (only used with schema attributes)
象征XML的ID属性的字符串(只用在schema属性里)
IDREF A string that represents the IDREF attribute in XML (only used with schema attributes)
象征XML里的IDREF属性的字符串(只能和schema属性一起使用)
IDREFS
language 原文链接:https://www.f2er.com/xml/297999.html