进阶day13-XML的约束文件-schema

This commit is contained in:
2026-04-11 14:01:09 +08:00
parent 0b31c10927
commit fb065125e1
2 changed files with 64 additions and 0 deletions

40
javaSE-day13/books.xsd Normal file
View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
schema约束文件的根标签必须是schema
目标命名空间:类似java中的包名,用来在执行的xml中来导入约束文件
targetNamespace="http://www.example.org/books"
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/books"
elementFormDefault="qualified">
<!--
用来定义xml的根标签必须是books
-->
<element name="books">
<!--
声明books是复杂标签
复杂标签:拥有子标签或者属性的标签
-->
<complexType>
<!--
通过sequence标签指定子标签的顺序
用来指定books的子标签有哪些
-->
<sequence>
<element name="book" maxOccurs="unbounded" minOccurs="1">
<!--设置book标签是复杂标签-->
<complexType>
<!--设置book标签的子标签顺序-->
<sequence>
<element name="name" type="string"></element>
<element name="price" type="string"></element>
<element name="author" type="string"></element>
</sequence>
<!--设置book标签有属性叫bid-->
<attribute name="bid"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<books xmlns="http://www.example.org/books">
<book bid="j10">
<name>java快速入门1</name>
<price>99</price>
<author>张三</author>
</book>
<book bid="j10">
<name>java快速入门1</name>
<price>99</price>
<author>张三</author>
</book>
<book bid="j10">
<name>java快速入门1</name>
<price>99</price>
<author>张三</author>
</book>
<book bid="j10">
<name>java快速入门1</name>
<price>99</price>
<author>张三</author>
</book>
</books>