zoukankan      html  css  js  c++  java
  • XML Schema


    http://www.ibm.com/developerworks/cn/xml/x-schema/part1/index.html

    XML Schema 是基于 XML 的 DTD 替代者。

    XML Schema 可描述 XML 文档的结构。

    XML Schema 语言也可作为 XSD(XML Schema Definition)来引用。

    <schema> 元素是每一个 XML Schema 的根元素。

    ML 文档可对 DTD 或 XML Schema 进行引用。

    一个简单的 XML 文档:

    请看这个名为 "note.xml" 的 XML 文档:

    <?xml version="1.0"?>
    <note>
    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
    </note>DTD 文件

    下面这个例子是名为 "note.dtd" 的 DTD 文件,它对上面那个 XML 文档的元素进行了定义:

    <!ELEMENT note (to, from, heading, body)>
    <!ELEMENT to (#PCDATA)>
    <!ELEMENT from (#PCDATA)>
    <!ELEMENT heading (#PCDATA)>
    <!ELEMENT body (#PCDATA)>

    第 1 行定义 note 元素有四个子元素:"to, from, heading, body"。

    第 2-5 行定义了 to, from, heading, body 元素的类型是 "#PCDATA"。

    XML Schema

    下面这个例子是一个名为 "note.xsd" 的 XML Schema 文件,它定义了上面那个 XML 文档的元素:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3school.com.cn"
    xmlns="http://www.w3school.com.cn"
    elementFormDefault="qualified">

    <xs:element name="note">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="to" type="xs:string"/>
    <xs:element name="from" type="xs:string"/>
    <xs:element name="heading" type="xs:string"/>
    <xs:element name="body" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

    note 元素是一个复合类型,因为它包含其他的子元素。其他元素 (to, from, heading, body) 是简易类型,因为它们没有包含其他元素。您将在下面的章节学习更多有关复合类型和简易类型的知识。

    对 DTD 的引用

    此文件包含对 DTD 的引用:

    <?xml version="1.0"?>
    <!DOCTYPE note SYSTEM "http://www.w3school.com.cn/dtd/note.dtd">
    <note>
    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
    </note>对 XML Schema 的引用

    此文件包含对 XML Schema 的引用:

    <?xml version="1.0"?>
    <note
    xmlns="http://www.w3school.com.cn"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
    </note> <schema> 元素

    <schema> 元素是每一个 XML Schema 的根元素:

    <?xml version="1.0"?>

    <xs:schema>

    ...
    ...

    </xs:schema>

    <schema> 元素可包含属性。一个 schema 声明往往看上去类似这样:

    <?xml version="1.0"?>

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3school.com.cn"
    xmlns="http://www.w3school.com.cn"
    elementFormDefault="qualified">

    ...
    ...
    </xs:schema>代码解释:

    下面的片断:

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

    显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:

    这个片断:

    targetNamespace="http://www.w3school.com.cn"

    显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。

    这个片断:

    xmlns="http://www.w3school.com.cn"

    指出默认的命名空间是 "http://www.w3school.com.cn"。

    这个片断:

    elementFormDefault="qualified"

    指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。

    在 XML 文档中引用 Schema

    此 XML 文档含有对 XML Schema 的引用:

    <?xml version="1.0"?>

    <note xmlns="http://www.w3school.com.cn"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

    <to>George</to>
    <from>John</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
    </note>代码解释:

    下面的片断:

    xmlns="http://www.w3school.com.cn"

    规定了默认命名空间的声明。此声明会告知 schema 验证器,在此 XML 文档中使用的所有元素都被声明于 "http://www.w3school.com.cn" 这个命名空间。

    一旦您拥有了可用的 XML Schema 实例命名空间:

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

    您就可以使用 schemaLocation 属性了。此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置:

    xsi:schemaLocation="http://www.w3school.com.cn note.xsd"
  • 相关阅读:
    Version Control System, IntelliJ IDEA, Create a Git Repository for a Project and push to multiple remotes
    SWOT, Challenges to upgrade to MacOS Big Sur (Version 11.0.1)
    How to extract text from PDF(Image) files, OCR
    How to install NetSuite SuiteCloud plug-in to WebStorm
    How to use NetSuite SDF CLI, CLI for Node.js on Mac, suitecloud command
    How to use NetSuite SDF to download bundles/components
    How to run a NetSuite scheduled script at every month end
    js 如何让forEach可以break?
    How to setup oAuth 1.0 in NetSuite RESTlet API 如何在NetSuite中设置RESTlet API的oAuth认证
    关于带宽与网速和用户体验
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400867.html
Copyright © 2011-2022 走看看