xml validation(四)

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

Section 5. XML Schema

Validation using schemas requires two documents: the schema document and the instance document.

<?xml version="1.0"?>

<memories

xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

xsi:noNamespaceSchemaLocation='memory.xsd'>

<memory tapeid="idnum">

...

noNamespaceSchemaLocation attribute to determine the location.

Structure of a schema document

 

< xsd:schema xmlns:xsd =" http://www.w3.org/2001/XMLSchema ">

            < xsd:element name =" memories ">

                        < xsd:complexType >

                                    < xsd:sequence >

                                                < xsd:element name =" memory " maxOccurs =" unbounded " type =" memoryType "/>

                                    </ xsd:sequence >

                        </ xsd:complexType >

            </ xsd:element >

           

< xsd:complexType name =" memoryType ">

            < xsd:sequence >

                        < xsd:element name =" media ">

                                    < xsd:complexType >

                                                < xsd:attribute name =" mediaid " type =" xsd:string " />

                                                < xsd:attribute name =" status " type =" xsd:string " />

                                    </ xsd:complexType >

                        </ xsd:element >

                        < xsd:element name =" subdate " type =" xsd:string "/>

                        < xsd:element name =" donor " type =" xsd:string "/>

                        < xsd:element name =" subject " type =" xsd:string "/>

                        < xsd:element name =" location " type =" locationType " />

            </ xsd:sequence >

            < xsd:attribute name =" tapeid " type =" xsd:string " />

            < xsd:attribute name =" status " type =" xsd:string " />

</ xsd:complexType >

 

< xsd:complexType name =" locationType ">

            < xsd:choice >

                        < xsd:element name =" description " type =" xsd:string " />

                        < xsd:element name =" place " type =" xsd:string " />

            </ xsd:choice >

</ xsd:complexType >

</ xsd:schema >

 

Elements and built-in types

< xsd:element name =" subdate " type =" xsd:date "/>

The subdate element has been further constrained, yyyy-mm-dd, as defined in the W3C XML Schema Recommendation.

Forty-two simple types are defined as part of the recommendation, including

string, int, date, decimal, boolean, timeDuration, and uriReference.

 

Simple types: restricting values

In addition to the built-in simple types, you can create new simple types.

For example: idNumber:

< xsd:simpleType name =" idNumber ">

            < xsd:restriction base =" xsd:integer ">

                        < xsd:minInclusive value =" 1 " />

                        < xsd:maxInclusive value =" 100000 " />

            </ xsd:restriction >

</ xsd:simpleType >

Simple types: enumeration

< xsd:simpleType name =" mediaType ">

            < xsd:restriction base =" xsd:string ">

                        < xsd:enumeration value =" 8mm " />

                        < xsd:enumeration value =" vhs " />

                        < xsd:enumeration value =" vhsc " />

                        < xsd:enumeration value =" digital " />

                        < xsd:enumeration value =" audio " />

            </ xsd:restriction >

</ xsd:simpleType >

Complex types: attributes

< xsd:element name =" media ">

            < xsd:complexType >

                        < xsd:attribute name =" mediaid " type =" xsd:integer " />

                        < xsd:attribute name =" status " type =" mediaType " />

            </ xsd:complexType >

</ xsd:element >

In this case, the media element now has two attributes, including one that follows the enumerated mediaType

Complex types: elements

< xsd:element name =" memories ">

            < xsd:complexType >

                        < xsd:sequence >

                                    < xsd:element name =" memory " type =" memoryType "/>

                        </ xsd:sequence >

            </ xsd:complexType >

</ xsd:element >

This example shows only one child element, but you still need sequence element.The type memoryType combines many of the techniques seen so far:

< xsd:complexType name =" memoryType ">

            < xsd:sequence >

                        < xsd:element name =" media ">

                                    < xsd:complexType >

                                                < xsd:attribute name =" mediaid " type =" xsd:integer " />

                                                < xsd:attribute name =" status " type =" mediaType " />

                                    </ xsd:complexType >

                        </ xsd:element >

                        < xsd:element name =" subdate " type =" xsd:date "/>

                        < xsd:element name =" donor " type =" xsd:string "/>

                        < xsd:element name =" subject " type =" xsd:string "/>

                        < xsd:element name =" location " type =" locationType " />

            </ xsd:sequence >

            < xsd:attribute name =" tapeid " type =" idNumber " />

            < xsd:attribute name =" status " type =" xsd:string " />

</ xsd:complexType >

The memoryType type also includes a reference to the locationType , which allows the user to choose between potential children for an element.

Element choices

The sequence element shows all of the possible children of an element. The choice element can allow you choose only one element from a list of alternatives.

< xsd:complexType name =" locationType ">

            < xsd:choice >

                        < xsd:element name =" description " type =" xsd:string " />

                        < xsd:element name =" place " type =" xsd:string " />

            </ xsd:choice >

</ xsd:complexType >

Optional and repeated elements

At this point all elements and attributes added to the schema must appear exactly once. Using minOccurs and maxOccurs , you can control whether or not an item must appear, and whether or not it may repeat.

< xsd:element name =" subject " minOccurs =" 1 " maxOccurs =" 5 " type =" xsd:string "/>

subject element can appear as many as five times within a single element.

If you don’t want an upper limit.

< xsd:element name =" memories ">

            < xsd:complexType >

                        < xsd:sequence >

                                    < xsd:element name =" memory " minOccurs =" 0 " maxOccurs =" unbounded " type =" memoryType "/>

                        </ xsd:sequence >

            </ xsd:complexType >

</ xsd:element >

Mixed content in schemas

Sometimes an element contains mixed content , as opposed to just elements or just text. For example, the subject element might contain HTML markup:

< subject >

            A reading of Charles Dickens' < i > A Christmas Carol </ i > .

            Absolutely < b > marvelous </ b > !

</ subject >

This content could not be described as xsd:string because it contains elements. I and b cannot as element because they contain text. Instead, the subject element must be defined as mixed:

< xsd:element name =" subject ">

            < xsd:complexType mixed =" true ">

                        < xsd:sequence >

                                    < xsd:element name =" i " minOccurs =" 0 " maxOccurs =" unbounded " type =" xsd:string " />

                                    < xsd:element name =" b " minOccurs =" 0 " maxOccurs =" unbounded " type =" xsd:string " />

                        </ xsd:sequence >

            </ xsd:complexType >

</ xsd:element >

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值