当前位置: 首页 > article >正文

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


http://www.kler.cn/a/528137.html

相关文章:

  • 群晖搭建Gitea教程(使用系统自带的postgresql)
  • 攻防世界_simple_php
  • 6.二分算法
  • Day31-【AI思考】-深度学习方法论全解析——科学提升学习效率的终极指南
  • 使用 MSYS2 qemu 尝鲜Arm64架构国产Linux系统
  • 适合超多氛围灯节点应用的新选择
  • 使用 Ollama 部署 TinyLlama (1.1B) 的完整流程,包含直接拉取和手动部署两种方案
  • 电子电气架构 --- 汽车电子拓扑架构的演进过程
  • Android记事本App设计开发项目实战教程2025最新版Android Studio
  • ComfyUI中For Loop的使用
  • C++初阶 -- 手撕string类(模拟实现string类)
  • LeetCode:279.完全平方数
  • 2.1刷题日记
  • Java基础——分层解耦——IOC和DI入门
  • 介绍使用 WGAN(Wasserstein GAN)网络对天然和爆破的地震波形图进行分类的实现步骤
  • 走向基于大语言模型的新一代推荐系统:综述与展望
  • Python安居客二手小区数据爬取(2025年)
  • 吊打同类软件免费又可批量使用
  • 【RAG】SKLearnVectorStore 避免使用gpt4all会connection err
  • 92,[8] 攻防世界 web Web_php_wrong_nginx_config
  • 基于SpringBoot的医疗报销系统的设计与实现(源码+SQL脚本+LW+部署讲解等)
  • WebForms DataList 深入解析
  • Nginx 运维开发高频面试题详解
  • 浅析DNS污染及防范
  • 【cocos creator】【模拟经营】餐厅经营demo
  • 【Linux】使用VirtualBox部署Linux虚拟机