zoukankan      html  css  js  c++  java
  • 如何使用 XSD

    如何使用 XSD

    一个简单的 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>
  • 相关阅读:
    在实践中不断总结和提升
    [转]态度的魔力 Net
    回答的智慧 Net
    [转] 【领导必读】唐僧为什么可以领导孙悟空 Net
    [转载]人生感悟:8个笑话 8味人生 Net
    人生成功的十大说话技巧 Net
    最新人生感悟语句摘选 Net
    2012注定是收获的一年,奋斗才刚刚开始
    程序员职业发展的绊脚石思想的枷锁
    AgileEAS.NET5.0界面设计器使用说明书(上)
  • 原文地址:https://www.cnblogs.com/mjorcen/p/3653187.html
Copyright © 2011-2022 走看看