2、创建”学生信息.xsd”,根元素为”学生信息”,其子元素1到多个”学生”。各学生有属性”学号”为ID类型,该属性是必需的,学生元素有子元素”姓名”,”性别”,”年龄”,个子元素有序出现一次。其中”姓名”为字符串类型,字符串长度大于等于2小于等于10,”性别”为枚举类型,取值“男”或“女”。”年龄”为整数型,取值大于等于18小于等于35。由此xsd创建XML文档”学生信息.xml”创建2个学生的信息,并验证有效性。将创建的两个文件保存到第二题文件夹中。
时间: 2024-02-23 14:58:14 浏览: 21
以下是符合题目要求的 XSD 文件内容:
```
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="学生信息" type="学生信息类型"/>
<xs:complexType name="学生信息类型">
<xs:sequence>
<xs:element name="学生" type="学生类型" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="学生类型">
<xs:sequence>
<xs:element name="姓名" type="xs:string" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:documentation>姓名,字符串类型,长度大于等于2小于等于10</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="性别" type="性别类型" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:documentation>性别,枚举类型,取值“男”或“女”</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="年龄" type="xs:integer" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:documentation>年龄,整数型,取值大于等于18小于等于35</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
<xs:attribute name="学号" type="xs:ID" use="required">
<xs:annotation>
<xs:documentation>学号,ID类型,必需属性</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:simpleType name="性别类型">
<xs:restriction base="xs:string">
<xs:enumeration value="男"/>
<xs:enumeration value="女"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
```
以下是符合题目要求的 XML 文件内容:
```
<?xml version="1.0" encoding="UTF-8"?>
<学生信息 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="学生信息.xsd">
<学生 学号="1001">
<姓名>张三</姓名>
<性别>男</性别>
<年龄>18</年龄>
</学生>
<学生 学号="1002">
<姓名>李四</姓名>
<性别>女</性别>
<年龄>20</年龄>
</学生>
</学生信息>
```
请注意,以上 XML 文件中的 `xsi:noNamespaceSchemaLocation` 属性指定了 XSD 文件的位置,需要将 XSD 文件和 XML 文件放在同一目录下。在验证 XML 文件的有效性时,需要使用以下命令:
```
xmllint --schema 学生信息.xsd 学生信息.xml
```
其中 `xmllint` 是一个常用的 XML 验证工具,可以在终端中运行。如果 XML 文件合法,命令行会输出 `学生信息.xml validates`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)