zoukankan      html  css  js  c++  java
  • XML Schema教程

    XML Schema教程

    什么是Schema

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

    学习目标

    • 读取XML Schema

    • 创建XML Schema

    • 使用XML Schema


    示例:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <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>
    <!--xs:再xml中的概念就是命名空间-->

    XML Schema简介

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

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

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

    什么是XML Schema

    定义:

    • 基于XML的DTD替代者

    作用:

    • 定义 XML 文档的合法构建模块,类似 DTD。

    XML Schema:

    • 定义可出现在文档中的元素

    • 定义可出现在文档中的属性

    • 定义哪个元素是子元素

    • 定义子元素的次序

    • 定义子元素的数目

    • 定义元素是否为空,或者是否可包含文本

    • 定义元素和属性的数据类型

    • 定义元素和属性的默认值以及固定值

    XSD的使用

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

    实例:

    <?xml version="1.0"?>
    <note>
     <to>Tove</to>
     <from>Jani</from>
     <heading>Reminder</heading>
     <body>Don't forget me this weekend!</body>
    </note>
    <!ELEMENT note (to, from, heading, body)>
    <!ELEMENT to (#PCDATA)>
    <!ELEMENT from (#PCDATA)>
    <!ELEMENT heading (#PCDATA)>
    <!ELEMENT body (#PCDATA)>

    特点:

    • 名为 "note.dtd" 的 DTD 文件,它对上面那个 XML 文档( "note.xml" )的元素进行了定义

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

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

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com"
    xmlns="http://www.w3schools.com"
    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) 是简易类型,因为它们没有包含其他元素


    下面是XML对它们的引用:

    对DTD的引用

    <?xml version="1.0"?>

    <!DOCTYPE note SYSTEM
    "http://www.w3schools.com/dtd/note.dtd">

    <note>
     <to>Tove</to>
     <from>Jani</from>
     <heading>Reminder</heading>
     <body>Don't forget me this weekend!</body>
    </note>

    对XML Schema的引用

    <?xml version="1.0"?>

    <note
    xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com note.xsd">
     <to>Tove</to>
     <from>Jani</from>
     <heading>Reminder</heading>
     <body>Don't forget me this weekend!</body>
    </note>

     

    It's a lonely road!!!
  • 相关阅读:
    [CSP校内集训]2019.10.16数学专题
    Knights of the Round Table(缩点+判奇环) poj 2942 && 洛谷SP2878
    机房测试7:exam(二进制+模拟)
    机房测试9:gift(单调队列优化dp)
    机房测试9:hotel(神奇dp)
    机房测试8:question(求最大1矩阵:悬线法 or 二分)
    机房测试7:paint(分治+st表)
    机房测试6:矿石(优先队列)
    机房测试6:括号序列(hash+栈 )
    bzoj1123 && 洛谷 P3469 tarjan割点的运用
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/14641867.html
Copyright © 2011-2022 走看看