实验四 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、编写实验报告并提交至超星。

相关推荐
历程里程碑8 小时前
Protobuf vs JSON vs XML:小白该怎么选?
xml·大数据·数据结构·elasticsearch·链表·搜索引擎·json
那个失眠的夜1 天前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis
mfxcyh1 天前
基于xml、注解、JavaConfig实现spring的ioc
xml·java·spring
vortex52 天前
SOAP 协议中的 XML 外部实体注入(XXE)漏洞
xml·网络安全·渗透测试
Dxy12393102162 天前
Python如何对XML进行格式化
xml·python
2501_930707783 天前
使用C#代码将 HTML 转换为 PDF、XPS 和 XML
xml·pdf
研來如此7 天前
tinyxml2 常用读取接口对照表
xml·c++·tinyxml2
pupudawang8 天前
使用 Logback 的最佳实践:`logback.xml` 与 `logback-spring.xml` 的区别与用法
xml·spring·logback
jf加菲猫9 天前
第10章 数据处理
xml·开发语言·数据库·c++·qt·ui
Java成神之路-9 天前
序列化协议全解析:XML、SOAP、JSON 与 Protobuf 实战对比及 Protobuf 演进方案
xml·json