目录
〇、XML课程设计要求
1.课程设计说明:
1).选取某一场景,描述该场景下的实体、实体属性以及实体之间的关系;注意选取实体最好体现出分解层次,以便于呈现XML,XML本身遵循层次数据模型;该部分文本描述要详尽,并用表格呈现出来。(10分)
2).采用XML对上述实体、实体属性及关系进行描述,要求体现出系统预定义实体,保证XML语法正确。(10分)
3).依据实例化XML文档写出组织模板文件DTD和SCHEMA,要求DTD与SCHEMA要保持映射,同时证明XML文件为有效的文档,同时组织模板要具有较高的抽象层次和较宽泛的约束能力。(40分)
4).提出4个查询题设,要求至少两个要结合XPATH标准函数,写出XPATH求解路径。(20分)
5).利用XSLT显示模板将XML文件展示为表格,与1一致。(10分)
6).利用JAVA或PYTHON等工具对XML文件节点进行属性值读写操作。(10分)
2.课程设计要求
1).任务1-5为必做,任务6为选做;如果任务6不做,则相应分值分配给任务3和4各5分,如果做则任务6按照现有10分来评分。请在提交课程设计开始就说明是否完成任务6。
2).每个任务评分均分为四个等级,优良中差,各任务合计之后为最终课程设计分数。
3.课程设计格式和任务之间的衔接也作为课程设计考察的一部分。
一、场景选取
1.场景描述
以学院为例,一个学院拥有多个专业,一个专业拥有多个班级和多个教师。一个班级拥有多个学生和一个(唯一)班长,一个老师可以教授多个课程。
例如,信息管理学院有三个专业,分别是信息资源管理、电子商务和信息管理与信息系统。(假设)每一个专业都有两个班级和两个教师,每一个班级都有一名班长和一名学生,每一名教师教授两门课程。
2.层次数据模型
3.用表格表示
二、XML文档编写
1. XML文档代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2014 (http://www.altova.com) by () -->
<!-- 以下数据都是虚拟出来的数据,并不真实-->
<School ID="IOS001" name="信息管理学院">
<Major ID="M001" name="信息资源管理">
<Teacher TID="T001">
<pname>陈思考</pname>
<sex>男</sex>
<professionalpost>教授</professionalpost>
<Course CNO="C001">
<cname>文献信息资源检索</cname>
<credit>2</credit>
<period>32</period>
</Course>
<Course CNO="C002">
<cname>管理信息系统</cname>
<credit>4</credit>
<period>48</period>
</Course>
</Teacher>
<Teacher TID="T002">
<pname>张简单</pname>
<sex>女</sex>
<professionalpost>副教授</professionalpost>
<Course CNO="C003">
<cname>高等数学B</cname>
<credit>2</credit>
<period>32</period>
</Course>
<Course CNO="C004">
<cname>管理统计学</cname>
<credit>2</credit>
<period>32</period>
</Course>
</Teacher>
<Class CID="1901" name="信资1班">
<Classpresident SID="S001">
<pname>戴二刚</pname>
<sex>男</sex>
</Classpresident>
<Student SID="S002">
<pname>江福</pname>
<sex>女</sex>
</Student>
</Class>
<Class CID="1902" name="信资2班">
<Classpresident SID="S003">
<pname>刘欢</pname>
<sex>女</sex>
</Classpresident>
<Student SID="S004">
<pname>严几</pname>
<sex>男</sex>
</Student>
</Class>
</Major>
<Major ID="M002" name="电子商务">
<Teacher TID="T003">
<pname>王得</pname>
<sex>男</sex>
<professionalpost>教授</professionalpost>
<Course CNO="C005">
<cname>数据挖掘</cname>
<credit>3</credit>
<period>36</period>
</Course>
<Course CNO="C006">
<cname>经济学基础</cname>
<credit>2</credit>
<period>32</period>
</Course>
</Teacher>
<Teacher TID="T004">
<pname>王默默</pname>
<sex>男</sex>
<professionalpost>副教授</professionalpost>
<Course CNO="C007">
<cname>管理运筹学</cname>
<credit>2</credit>
<period>32</period>
</Course>
<Course CNO="C008">
<cname>管理统计学</cname>
<credit>2</credit>
<period>32</period>
</Course>
</Teacher>
<Class CID="1903" name="电商1班">
<Classpresident SID="S005">
<pname>陈凡</pname>
<sex>男</sex>
</Classpresident>
<Student SID="S006">
<pname>石发</pname>
<sex>男</sex>
</Student>
</Class>
<Class CID="1904" name="电商2班">
<Classpresident SID="S007">
<pname>吴宇</pname>
<sex>女</sex>
</Classpresident>
<Student SID="S008">
<pname>葛一萨</pname>
<sex>男</sex>
</Student>
</Class>
</Major>
<Major ID="M003" name="信息管理与信息系统">
<Teacher TID="T005">
<pname>刘玉</pname>
<sex>女</sex>
<professionalpost>教授</professionalpost>
<Course CNO="C009">
<cname>Java:面向对象程序设计</cname>
<credit>4</credit>
<period>48</period>
</Course>
<Course CNO="C010">
<cname>数据结构(C/C++)</cname>
<credit>3</credit>
<period>36</period>
</Course>
</Teacher>
<Teacher TID="T006">
<pname>史丹</pname>
<sex>女</sex>
<professionalpost>副教授</professionalpost>
<Course CNO="C011">
<cname>XML</cname>
<credit>2</credit>
<period>32</period>
</Course>
<Course CNO="C012">
<cname>UML</cname>
<credit>2</credit>
<period>32</period>
</Course>
</Teacher>
<Class CID="1905" name="信管1班">
<Classpresident SID="S009">
<pname>得得</pname>
<sex>男</sex>
</Classpresident>
<Student SID="S010">
<pname>陈生</pname>
<sex>女</sex>
</Student>
</Class>
<Class CID="1906" name="信管2班">
<Classpresident SID="S011">
<pname>储梦</pname>
<sex>女</sex>
</Classpresident>
<Student SID="S012">
<pname>撒方法</pname>
<sex>男</sex>
</Student>
</Class>
</Major>
</School>
2. 格式良好验证
3. grid视图
部分数据的效果展示。
三、DTD和SCHEMA
1.DTD文件
1)DTD代码。
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT School (Major)+>
<!ELEMENT Major (Teacher+,Class+)>
<!ELEMENT Teacher (pname,sex,professionalpost,Course+)>
<!--参数内部解析实体-->
<!ENTITY % pd "(#PCDATA)">
<!ELEMENT pname %pd;>
<!ELEMENT sex %pd;>
<!ELEMENT professionalpost %pd;>
<!ELEMENT Course (cname,credit,period)>
<!ELEMENT cname %pd;>
<!ELEMENT credit %pd;>
<!ELEMENT period %pd;>
<!ELEMENT Class (Classpresident,Student*)>
<!ELEMENT Classpresident (pname,sex)>
<!ELEMENT Student (pname,sex)>
<!ATTLIST School ID ID #REQUIRED
name CDATA #REQUIRED>
<!ATTLIST Major ID ID #REQUIRED
name CDATA #REQUIRED>
<!ATTLIST Teacher TID ID #REQUIRED>
<!ATTLIST Class CID CDATA #REQUIRED
name CDATA #REQUIRED>
<!ATTLIST Course CNO ID #REQUIRED>
<!ATTLIST Classpresident SID ID #REQUIRED>
<!ATTLIST Student SID ID #REQUIRED>
2)使用DTD进行语法约束的XML文档,通过了有效性检验。
2.SCHEMA文件
1)SCHEMA代码。
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2014 (http://www.altova.com) by () -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="School">
<xs:annotation>
<xs:documentation>该节点为根节点,表示学院</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<!--语法3,引用元素-->
<xs:element ref="Major" minOccurs="1" maxOccurs="unbounded"></xs:element>
</xs:sequence>
<xs:attributeGroup ref="idname"></xs:attributeGroup>
</xs:complexType>
</xs:element>
<xs:element name="Major">
<xs:complexType>
<xs:sequence>
<xs:element ref="Teacher" minOccurs="1" maxOccurs="unbounded"></xs:element>
<xs:element ref="Class" minOccurs="1" maxOccurs="unbounded"></xs:element>
</xs:sequence>
<xs:attributeGroup ref="idname"></xs:attributeGroup>
</xs:complexType>
</xs:element>
<xs:element name="Teacher">
<xs:complexType>
<xs:sequence>
<xs:element ref="pname" maxOccurs="1"></xs:element>
<xs:element ref="sex" maxOccurs="1"></xs:element>
<xs:element ref="professionalpost" maxOccurs="1"></xs:element>
<xs:element ref="Course" minOccurs="1" maxOccurs="unbounded"></xs:element>
</xs:sequence>
<xs:attribute name="TID" type="xs:ID"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Course">
<xs:complexType>
<xs:sequence>
<xs:element ref="cname" maxOccurs="1"></xs:element>
<xs:element ref="credit" maxOccurs="1"></xs:element>
<xs:element ref="period" maxOccurs="1"></xs:element>
</xs:sequence>
<xs:attribute name="CNO" type="xs:ID"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Class">
<xs:complexType>
<xs:sequence>
<xs:element ref="Classpresident" maxOccurs="1"></xs:element>
<xs:element ref="Student" maxOccurs="unbounded"></xs:element>
</xs:sequence>
<xs:attribute name="CID" type="xs:string"></xs:attribute>
<xs:attribute name="name" type="xsname"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Student">
<xs:complexType>
<xs:sequence>
<xs:element ref="pname" maxOccurs="1"></xs:element>
<xs:element ref="sex" maxOccurs="1"></xs:element>
</xs:sequence>
<xs:attribute name="SID" type="xs:ID"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Classpresident">
<xs:complexType>
<xs:sequence>
<xs:element ref="pname" maxOccurs="1"></xs:element>
<xs:element ref="sex" maxOccurs="1"></xs:element>
</xs:sequence>
<xs:attribute name="SID" type="xs:ID"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="pname" type="atrpname"></xs:element>
<xs:element name="sex" type="atrsex"></xs:element>
<xs:element name="professionalpost" type="atrpp"></xs:element>
<xs:element name="cname" type="atrcname"></xs:element>
<xs:element name="credit" type="atrcredit"></xs:element>
<xs:element name="period" type="atrperiod"></xs:element>
<!--用户自定义全局简单数据类型-->
<xs:simpleType name="xsname">
<xs:restriction base="xs:string">
<xs:minLength value="2"></xs:minLength>
<xs:maxLength value="10"></xs:maxLength>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="atrpname">
<xs:restriction base="xs:string">
<xs:minLength value="2"></xs:minLength>
<xs:maxLength value="20"></xs:maxLength>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="atrsex">
<xs:restriction base="xs:string">
<xs:enumeration value="男"></xs:enumeration>
<xs:enumeration value="女"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="atrpp">
<xs:restriction base="xs:string">
<xs:enumeration value="教授"></xs:enumeration>
<xs:enumeration value="副教授"></xs:enumeration>
<xs:enumeration value="讲师"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="atrperiod">
<xs:restriction base="xs:int">
<xs:minExclusive value="24"></xs:minExclusive>
<xs:maxExclusive value="72"></xs:maxExclusive>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="atrcname">
<xs:restriction base="xs:string">
<xs:minLength value="3"></xs:minLength>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="atrcredit">
<xs:restriction base="xs:short">
<xs:minExclusive value="1"></xs:minExclusive>
<xs:maxExclusive value="6"></xs:maxExclusive>
</xs:restriction>
</xs:simpleType>
<!--定义属性组,范围全局-->
<xs:attributeGroup name="idname">
<xs:attribute name="ID" type="xs:ID"/>
<xs:attribute name="name" type="xsname"/>
</xs:attributeGroup>
</xs:schema>
2)使用SCHEMA进行语法约束的XML文档,通过了有效性检验。
四、XPATH
1.查看所有老师的姓名
1)语法:
全路径
/child::School/child::Major/child::Teacher/child::pname/child::text()
简化路径
2)结果:
2.查看学时大于等于32课时的课程
1)语法:
全路径
/child::School/child::Major/child::Teacher/child::Course/child::period[text()>=36]/parent::Course/child::cname
简化路径
2)结果:
3.查看包含XML字段的课程号
1)语法:
/School/Major/Teacher/Course/cname[contains(text(),'XML')]/parent::Course
2)结果:
4.统计学院老师人数
1)语法:
/School/count(Major/Teacher)
2)结果:
5.统计姓陈的学生人数
1)语法:
count(/School/Major/Class/Classpresident/pname[contains(text(),'陈')])+
count(/School/Major/Class/Student/pname[contains(text(),'陈')])
2)结果:
五、XSLT显示模板
1.XSLT文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/School">
<html>
<head>
<title>XXXX学院</title>
</head>
<body>
<div>XXXX学院</div>
<ul>
<xsl:apply-templates select="Major" mode="a"/>
</ul>
<table border="1">
<tr>
<td colspan="4" style="font-weight:bold">教师信息表</td>
</tr>
<tr>
<td>姓名</td>
<td>性别</td>
<td>职称</td>
<td>所属系</td>
</tr>
<!--调用模板规则-->
<xsl:apply-templates select="Major" mode="teacher"/>
</table>
<br/>
<table border="1">
<tr>
<td colspan="5" style="font-weight:bold">学生信息表</td>
</tr>
<tr>
<td>姓名</td>
<td>性别</td>
<td>职位</td>
<td>所属班级</td>
<td>所属系</td>
</tr>
<!--调用模板规则-->
<xsl:apply-templates select="Major/Class" mode="student"/>
</table>
<br/>
<table border="1">
<tr>
<td colspan="4" style="font-weight:bold">课程信息表</td>
</tr>
<tr>
<td>课程名</td>
<td>学分</td>
<td>学时</td>
<td>授课教师</td>
</tr>
<!--调用模板规则-->
<xsl:apply-templates select="Major/Teacher" mode="course"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Major" mode="teacher">
<tr>
<td><xsl:value-of select="Teacher[1]/pname"/></td>
<td><xsl:value-of select="Teacher[1]/sex"/></td>
<td><xsl:value-of select="Teacher[1]/professionalpost"/></td>
<td><xsl:value-of select="Teacher[1]/parent::node()/@attribute(name)"/></td>
</tr>
<tr>
<td><xsl:value-of select="Teacher[2]/pname"/></td>
<td><xsl:value-of select="Teacher[2]/sex"/></td>
<td><xsl:value-of select="Teacher[2]/professionalpost"/></td>
<td><xsl:value-of select="Teacher[2]/parent::node()/@attribute(name)"/></td>
</tr>
</xsl:template>
<xsl:template match="Major/Class" mode="student">
<tr>
<td><xsl:value-of select="Classpresident/pname"/></td>
<td><xsl:value-of select="Classpresident/sex"/></td>
<td>班长</td>
<td><xsl:value-of select="Classpresident/parent::node()/@attribute(name)"/></td>
<td><xsl:value-of select="Classpresident/parent::node()/parent::node()/@attribute(name)"/></td>
</tr>
<tr>
<td><xsl:value-of select="Student[1]/pname"/></td>
<td><xsl:value-of select="Student[1]/sex"/></td>
<td>学生</td>
<td><xsl:value-of select="Student[1]/parent::node()/@attribute(name)"/></td>
<td><xsl:value-of select="Student[1]/parent::node()/parent::node()/@attribute(name)"/></td>
</tr>
</xsl:template>
<xsl:template match="Major/Teacher" mode="course">
<tr>
<td><xsl:value-of select="Course[1]/cname"/></td>
<td><xsl:value-of select="Course[1]/credit"/></td>
<td><xsl:value-of select="Course[1]/period"/></td>
<td><xsl:value-of select="Course[1]/parent::node()/pname"/></td>
</tr>
<tr>
<td><xsl:value-of select="Course[2]/cname"/></td>
<td><xsl:value-of select="Course[2]/credit"/></td>
<td><xsl:value-of select="Course[2]/period"/></td>
<td><xsl:value-of select="Course[2]/parent::node()/pname"/></td>
</tr>
</xsl:template>
<xsl:template match="Major" mode="a">
<li><xsl:value-of select="@attribute(name)"/>
<ul>
<li>班级
<ul>
<li><xsl:value-of select="Class[1]/@attribute(name)"/></li>
<li><xsl:value-of select="Class[2]/@attribute(name)"/></li>
</ul>
</li>
<li>教师
<ul>
<li><xsl:value-of select="Teacher[1]/pname"/></li>
<li><xsl:value-of select="Teacher[2]/pname"/></li>
</ul>
</li>
</ul>
</li>
</xsl:template>
</xsl:stylesheet>
2.转换后的XML文件
3.在浏览器的显示结果:
六、JAVA工具
1.加载包和其他
2.读取操作
3.写入操作
4.main函数及运行结果
1)Main函数。
2)运行结果。
3)新建的xml文件。
想要文件的可以留言哟~