实验四 XML

实验四 XML

目的

1、安装和使用XML的开发环境

2、认识XML的不同类型

3、掌握XML文档的基本语法

4、了解DTD的作用

5、掌握DTD的语法

6、掌握Schema的语法

实验过程

1、安装XML的编辑器,可以选择以下之一

a)XMLSpy

b)VScode,Vscode中安装XML插件

2、给定一个XML文档test.xml

复制代码
<?xml version="1.0"?>
<students>
    <student id="001">
        <name>tom</name>
        <age>24</age>
        <major>
            <course cid="c1">Python</course>
        </major>
        <phone>18611111111</phone>
        <phone>18622222222</phone>
    </student>
    <student id="002">
        <name>sammy</name>
        <age>25</age>
        <major>
            <course cid="c2">C++</course>
            <course cid="c3">computer principle</course>
        </major>
        <phone>18633333333</phone>
    </student>
</students>

a)为test.xml定义一个内部的DTD,写出完整的xml文档

如下为进行DTD约束后的完整xml代码:

复制代码
<?xml version="1.0"?>
<!DOCTYPE students  [
        <!ELEMENT students (student*)>
        <!ELEMENT student (name,age,major+,phone+)>
        <!ELEMENT major (course+)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT age (#PCDATA)>
        <!ELEMENT phone (#PCDATA)>
        <!ELEMENT course (#PCDATA)>
        <!ATTLIST student id ID #REQUIRED>
        <!ATTLIST course cid ID #REQUIRED>
        ]>
<students>
    <student id="s001">
        <name>tom</name>
        <age>24</age>
        <major>
            <course cid="c1">Python</course>
        </major>
        <phone>18611111111</phone>
        <phone>18622222222</phone>
    </student>

    <student id="s002">
        <name>sammy</name>
        <age>25</age>
        <major>
            <course cid="c2">C++</course>
            <course cid="c3">computer principle</course>
        </major>
        <phone>18633333333</phone>
    </student>
</students>

b)为test.xml定义一个外部的Schema文档,写出schema文档和使用schema文档后的test.xml

如下是Schema文档代码和导入外部Schema约束的xml完整代码

Schema文档代码(test.xsd):

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.zhjTest.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="students" type="zhj:studentsType" xmlns:zhj="http://www.zhjTest.cn"/>
  <xs:complexType name="courseType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="cid" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="majorType">
    <xs:sequence>
      <xs:element type="zhj:courseType" name="course" maxOccurs="unbounded" minOccurs="0" xmlns:zhj="http://www.zhjTest.cn"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="studentType">
    <xs:sequence>
      <xs:element name="name">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="tom"/>
            <xs:enumeration value="sammy"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="age">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="24"/>
            <xs:enumeration value="25"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element type="zhj:majorType" name="major" xmlns:zhj="http://www.zhjTest.cn"/>
      <xs:element name="phone" maxOccurs="unbounded" minOccurs="0">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="18611111111"/>
            <xs:enumeration value="18622222222"/>
            <xs:enumeration value="18633333333"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
    <xs:attribute type="xs:string" name="id" use="optional"/>
  </xs:complexType>
  <xs:complexType name="studentsType">
    <xs:sequence>
      <xs:element type="zhj:studentType" name="student" maxOccurs="unbounded" minOccurs="0" xmlns:zhj="http://www.zhjTest.cn"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

xml完整代码:

复制代码
<?xml version="1.0"?>
<!DOCTYPE students  [
        <!ELEMENT students (student*)>
        <!ELEMENT student (name,age,major+,phone+)>
        <!ELEMENT major (course+)>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT age (#PCDATA)>
        <!ELEMENT phone (#PCDATA)>
        <!ELEMENT course (#PCDATA)>
        <!ATTLIST student id ID #REQUIRED>
        <!ATTLIST course cid ID #REQUIRED>
        ]>
<students xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://www.zhjTest.cn"
          xs:schemaLocation="http://www.zhjTest.cn test.xsd"
>
    <student id="s001">
        <name>tom</name>
        <age>24</age>
        <major>
            <course cid="c1">Python</course>
        </major>
        <phone>18611111111</phone>
        <phone>18622222222</phone>
    </student>

    <student id="s002">
        <name>sammy</name>
        <age>25</age>
        <major>
            <course cid="c2">C++</course>
            <course cid="c3">computer principle</course>
        </major>
        <phone>18633333333</phone>
    </student>
</students>

3、编写实验报告并提交至超星。

相关推荐
emma羊羊1 天前
【weblogic】XML反序列化漏洞
xml·安全
研來如此3 天前
XML与HTML
xml·html
一个W牛3 天前
报文比对工具(xml和sop)
xml·前端·javascript
Java&Develop3 天前
使用 JDOM 库生成 XML 文件并返回 File
xml
关关长语3 天前
Dotnet使用System.Xml.Serialization处理Xml序列化
xml·c#·.net
Dolphin_Home3 天前
轻量实用的 XML 与 JSON / 对象互转工具类(Jackson 实现)
xml·java·json
huluang4 天前
XML文档差异分析工具:深入解析Word XML结构变化
xml·word
未孤_有青山5 天前
库卡机器人通讯-EtherKRL-XML格式
xml·c#
Luna-player6 天前
基于XML方式的声明式事务管理 -》某配置文件解读
xml
lang201509286 天前
Spring XML AOP配置实战指南
xml·java·spring