XML构架(转载) XSD Complex 空元素
发布时间:2020-12-16 09:33:24 所属栏目:百科 来源:网络整理
导读:An "elements-only" complex type contains an element that contains only other elements. “只有元素(Elements-only)”复合类型含有一个只包含其他元素的元素 Complex Types Containing Elements Only 复合类型只含有元素 An XML element,"person",that
An "elements-only" complex type contains an element that contains only other elements.
“只有元素(Elements-only)”复合类型含有一个只包含其他元素的元素
Complex Types Containing Elements Only 复合类型只含有元素 An XML element,"person",that contains only other elements: 一个XML元素,只含有其他元素: <person> <firstname>John</firstname> <lastname>Smith</lastname> </person> You can define the "person" element in a schema,like this: 你可以在一篇schema里定义"person"元素,就像这样 <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> Notice the <xs:sequence> tag. It means that the elements defined ("firstname" and "lastname") must appear in that order inside a "person" element. 注意<xs:sequence>标签。这表示所定义的元素("firstname" 和 "lastname")必须在"person"元素里以那样的次序出现。 Or you can give the complexType element a name,and let the "person" element have a type attribute that refers to the name of the complexType (if you use this method,several elements can refer to the same complex type): 或者你可以给complexType元素取个名字,让"person"元素有个类型属性,这个类型属性的名字可以参考使用complexType元素的名字(如果你用这个方法,几个元素可以同时参考使用相同的复合类型)。 <xs:element name="person" type="persontype"/> <xs:complexType name="persontype"> </xs:complexType> XSD 复合文字 - 纯元素 w3pop.com / 2006-09-21 XSD 复合类型 - 纯元素 XSD 混合内容的复合类型 A complex text-only element can contain text and attributes. 一个复合只含文本元素(Complex Text-Only Elements)可以含有文本和属性。 Complex Text-Only Elements 复合只含文本元素(Complex Text-Only Elements) This type contains only simple content (text and attributes),therefore we add a simpleContent element around the content. When using simple content,you must define an extension OR a restriction within the simpleContent element,51)">这种类型只含有简单内容(文本和属性),因此我们在内容周围添加一个simpleContent元素,当用到简单内容时,你必须在simpleContent元素里定义一个扩展或约束,就像这样: <xs:element name="somename"> <xs:simpleContent> <xs:extension base="basetype"> .... </xs:extension> </xs:simpleContent> OR <xs:restriction base="basetype"> </xs:restriction> Tip: Use the extension/restriction element to expand or to limit the base simple type for the element. 提示:用extension/restriction元素扩展或限制元素的基本简单类型(base simple type)。 Here is an example of an XML element,"shoesize",that contains text-only: 这儿是只含有文本的一个XML元素,"shoesize": <shoesize country="france">35</shoesize> The following example declares a complexType,"shoesize". The content is defined as an integer value,and the "shoesize" element also contains an attribute named "country": 下面的例子声明了一个复合类型,"shoesize"元素。内容定义为整数值,"shoesize"元件含有名为"country"的属性。 <xs:element name="shoesize"> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> We could also give the complexType element a name,and let the "shoesize" element have a type attribute that refers to the name of the complexType (if you use this method,51)">我们可以给complexType元件起个名字,让"shoesize"元件有种类属性,种类属性的名字就是complexType元件的名字。(如果你用这种方法,几个元素指的可以是相同的复合类型) <xs:element name="shoesize" type="shoetype"/> <xs:complexType name="shoetype"> XSD 混合内容的复合类型 XSD 复合文字 - 纯元素 XSD 指示器复合类型 A mixed complex type element can contain attributes,elements,and text. 混合内容的复合类型元素(XSD Complex Types Element With Mixed Content )可以含有属性,元素,和文本。 Complex Types with Mixed Content 混合内容的复合类型 一个XML元素,"letter",既含有文本又含有其他元素: <letter> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>. </letter> The following schema declares the "letter" element: 下面的XML公式声明了"letter"元素: <xs:element name="letter"> <xs:complexType mixed="true"> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> Note: To enable character data to appear between the child-elements of "letter",the mixed attribute must be set to "true". The <xs:sequence> tag means that the elements defined (name,orderid and shipdate) must appear in that order inside a "letter" element. 注意:为了使字符数据能出现在"letter"子元件之间,mixed属性必须设置为"true"。<xs:sequence>标签指出了已定义的元素(name,orderid 和shipdate)在"letter"元素里必须以指定的顺序出现 我们可以给这个complexType元素一个名称,并且让"letter"元素有一个引用了complexType的名称的种类属性(如果你用了这个方法,几个元素可以同时使用相同的复合类型): <xs:element name="letter" type="lettertype"/> <xs:complexType name="lettertype" mixed="true"> XSD 指示器复合类型 XSD 混合内容的复合类型 XSD <any> 元素 We can control HOW elements are to be used in documents with indicators. 用指示器(Indicators)我们可以控制文件中元素的使用方法(HOW TO USE)。 Indicators 指示器(Indicators) There are seven indicators: 有7种指示器(Indicators) Order indicators: 顺序指示器(Indicators) * All 全部 * Choice 选择 * Sequence 按顺序 Occurrence indicators: 出现次数指示器(Indicators): * maxOccurs 最多出现次数 * minOccurs 最少出现次数 Group indicators: 组指示器(Indicators): * Group name 组名 * attributeGroup name 属性组名称 Order Indicators Order indicators are used to define the order of the elements. 顺序指示器(Indicators)用于指定元素的顺序。 All Indicator 全部指示器(Indicators) The <all> indicator specifies that the child elements can appear in any order,and that each child element must occur only once: <all>指示器(Indicators)指明了子元件可以以任何次序出现,并且每个子元件只能出现一次: <xs:all> </xs:all> Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1 (the <minOccurs> and <maxOccurs> are described later). 注意:用<all> 指示器(Indicators)时你可以把<minOccurs>指示器(Indicators)设为0或1,< maxOccurs>指示器(Indicators)只能设为1(<minOccurs> 和 <maxOccurs> 我们后头再说) Choice Indicator 选择指示器(Indicators) The <choice> indicator specifies that either one child element or another can occur: <choice>指示器(Indicators)指明了随便的子元素都可以出现: <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> Sequence Indicator 有序指示器(Indicators) The <sequence> indicator specifies that the child elements must appear in a specific order: <sequence>指示器(Indicators)指定了子元素必须以一个指明的顺序出现: Occurrence Indicators 出现次数指示器(Indicators) Occurrence indicators are used to define how often an element can occur. 出现次数指示器(Indicators)用于定义一个元素可以出现的次数 Note: For all "Order" and "Group" indicators (any,all,choice,sequence,group name,and group reference) the default value for maxOccurs and minOccurs is 1. 注意:对所有的"Order"和"Group"指示器(Indicators)(any,sequence,group name,和 group reference)来说,maxOccurs 和 minOccurs的默认值都是1 maxOccurs Indicator 最多出现次数指示器(Indicators) The <maxOccurs> indicator specifies the maximum number of times an element can occur: 最多出现次数指示器(Indicators)指明了一个元素可以出现的最多次数: <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/> The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element. 上面的例子指明了"child_name"元素在"person"元素里最少出现1次(minOccurs的默认值为1),最多出现10次 minOccurs Indicator 最少出现次数指示器(Indicators) The <minOccurs> indicator specifies the minimum number of times an element can occur: 最少出现次数指示器(Indicators)指明了一个元素要出现的最小次数: <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/> The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element. 上面的例子指明了"child_name"元素在"person"元素里最少出现0次(minOccurs的默认值为1),最多出现10次 Tip: To allow an element to appear an unlimited number of times,use the maxOccurs="unbounded" statement: 提示:为使元件可以重复出现无数次,可以设置maxOccurs="unbounded"的状态 A working example: 实际作用例子: An XML file called "Myfamily.xml": 名为"Myfamily.xml"的XML文件: <?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="family.xsd"> <full_name>Hege Refsnes</full_name> <child_name>Cecilie</child_name> <full_name>Tove Refsnes</full_name> <child_name>Hege</child_name> <child_name>Stale</child_name> <child_name>Jim</child_name> <child_name>Borge</child_name> <full_name>Stale Refsnes</full_name> </persons> The XML file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements. 上面XML文件含有一个名为"persons"的根元素,这个根元素里面里我们已经定义了3个"person"元素。每个"person"元素必须含有一个"full_name"元素,而且最多可有5个"child_name"元素。 Here is the schema file "family.xsd": 下面是名为"family.xsd"的schema文件: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="persons"> <xs:element name="person" maxOccurs="unbounded"> minOccurs="0" maxOccurs="5"/> </xs:element> </xs:schema> Group Indicators 组指示器(Indicators) Group indicators are used to define related sets of elements. 组指示器(Indicators)用于定义相关的元素组。 Element Groups 元素组 Element groups are defined with the group declaration,51)">元素组要定义组声明,像这样: <xs:group name="groupname"> ... </xs:group> You must define an all,or sequence element inside the group declaration. The following example defines a group named "persongroup",that defines a group of elements that must occur in an exact sequence: 你必须在组声明里定义一个all,或sequence元素。下面的例子定义了一个名为"persongroup"的组,这个定义了一组元素必须以一定顺序出现: <xs:group name="persongroup"> <xs:element name="birthday" type="xs:date"/> After you have defined a group,you can reference it in another definition,51)">定义了一个组后,你可以在另一个组参考它,像这样: <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> Attribute Groups 属性组 Attribute groups are defined with the attributeGroup declaration,51)">属性组定义有attributeGroup(属性组)声明,像这样: <xs:attributeGroup name="groupname"> </xs:attributeGroup> The following example defines an attribute group named "personattrgroup": 下面的例子定义了叫做"personattrgroup"的一个属性组: <xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> After you have defined an attribute group,51)">定义了一个属性组之后,你可以在别的定义里参考它,像这样: <xs:attributeGroup ref="personattrgroup"/> </xs:element> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |