xml通过schema方式的文档定义xsd,及使用示例

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:tns="http://www.example.org/t3" elementFormDefault="qualified">
	<!--
		如果声明部分为: <schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
		则下面节点和属性必须加上xs:可以修改xs 。 默认为<schema
		xmlns="http://www.w3.org/2001/XMLSchema"
	-->
	<xs:element name="ISBN">
		<xs:simpleType>
			<xs:restriction base="xs:string">
				<xs:pattern value="[0-9]-[0-9]{3}-[0-9]{5}-[0-9]">
				</xs:pattern>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
	<xs:element name="author" type="xs:string">
	</xs:element>
	<xs:element name="bookName" type="xs:string">
	</xs:element>
	<xs:element name="price">
		<xs:simpleType>
			<!--xml schema中restriction定义可接受的值。 fractionDigits定义值的最大的小数位数,必须大于等于0-->
			<xs:restriction base="xs:decimal">
				<xs:fractionDigits value="2">
				</xs:fractionDigits>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
	<xs:element name="quantity">
		<xs:simpleType>
			<!--
				xml schema中restriction定义可接受的值。
				maxExclusive定义值的上限(大于),minExclusive定义值的下限(小于等于)。
			-->
			<xs:restriction base="xs:byte">
				<xs:maxExclusive value="100"></xs:maxExclusive>
				<xs:minExclusive value="0"></xs:minExclusive>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
	<xs:complexType name="bookType">
		<!-- xml schema中sequence定义值必须按此顺序出现-->
		<xs:sequence>
			<xs:element ref="bookName"></xs:element>
			<xs:element ref="author"></xs:element>
			<xs:element ref="ISBN"></xs:element>
			<xs:element ref="price"></xs:element>
			<xs:element ref="quantity"></xs:element>
		</xs:sequence>
		<xs:attribute name="id" use="required">
			<xs:simpleType>
				<xs:restriction base="xs:string">
					<xs:pattern value="[0-9]{3}"></xs:pattern>
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
		<xs:attribute name="type" use="required">
			<xs:simpleType>
				<!-- xml schema中restriction定义可接受的值。 enumeration定义可接受的值的列表。-->
				<xs:restriction base="xs:string">
					<xs:enumeration value="IT"></xs:enumeration>
					<xs:enumeration value="education"></xs:enumeration>
					<xs:enumeration value="novel"></xs:enumeration>
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
	</xs:complexType>
	<xs:element name="books">
		<!-- xml schema中complexType定义复合元素 -->
		<xs:complexType>
			<xs:sequence>
				<!--xml schema中 maxOccurs定义某个元素最多可出现的次数, minOccurs定义某个元素最少可出现的次数-->
				<xs:element name="book" type="bookType" maxOccurs="unbounded"
					minOccurs="1"></xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>


<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="t3.xsd">
	<book id="001" type="IT">
		<bookName>mastering XML</bookName>
		<author>Jack LEE</author>
		<ISBN>0-125-11111-6</ISBN>
		<price>36.00</price>
		<quantity>6</quantity>
	</book>
	<book id="002" type="education">
		<bookName>the method of study</bookName>
		<author>Thomas</author>
		<ISBN>0-125-22222-7</ISBN>
		<price>45.00</price>
		<quantity>5</quantity>
	</book>
	<book id="003" type="novel">
		<bookName>The hero</bookName>
		<author>TuDou</author>
		<ISBN>0-125-88888-6</ISBN>
		<price>88.00</price>
		<quantity>2</quantity>
	</book>
</books>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
附件是我写的一个demo程序,该程序的功能是根据一个定义好的XSD文件去检查指定的XML文件是否满足XSD的约束。 这里的约束比标准的约束要弱一些,比如:这里的约束不限制元素出现的顺序,只关心有没有这个元素。 我测试过的场景: 1. 在XSD文件中,为某个节点增加一个子节点,程序运行后,检查该子节点是否被添加(此时节点默认值为空字符串); 2. 在XSD文件中,为某个节点增加一个子节点,然后再为该节点定义一个默认值,程序运行后,检查该子节点是否被添加,以及他的默认值是否是XSD中指定的值; 3. 在XSD文件中,为某个节点增加一个属性,程序运行后,检查属性是否被添加;同样,如果为属性指定了默认值,该属性的默认值应为XSD中指定的值; 4. 在XSD文件中,为某个*可重复*的节点增加一个子节点,程序运行后,检查该子节点是否在所有的匹配元素中都被添加;同样如果为子节点指定了默认值,则新添加的子节点的默认值应为XSD中指定的值; 5. 在XSD文件中,为某个*可重复*的节点增加一个属性,程序运行后,检查该属性是否在所有匹配的元素中都被添加;同样如果为该属性指定了默认值,则新添加的属性的默认值应为XSD中指定的值。 6. 在XSD文件中,为某个节点增加一个带有属性的子节点,程序运行后,检查子节点是否被添加,以及子节点的属性是否被添加;同样如果为子节点和属性指定了默认值,则新添加的子节点和属性的默认值应为XSD中指定的值。 附件被解压后,有一个eclipse工程和两个文件:salary.xsd & salary.xml。这是我拿分箱处的XML的测试。 将这两个文件放到 c:/xml文件夹下面,运行工程中的test.upgrade.client.Upgrade.java,可以试试。 大家可以任意更改xsdxml,只要不: 1. 改变或删除XML的根节点名称; 2. 对XML更改后使其不符合XML的规范,比如:某个节点没有被闭合。 程序都还能保证对XML的更改使其满足XSD中规定的元素和属性。 也可以试试其他的XML文件,关于产生XML对应的XSD文件,可以从这个网址上转换一下: http://www.freeformatter.com/xsd-generator.html 我的做法是: 1 解析XSD文件,并将其规定的XML结构映射至自定义的Java类(XMLNode); 2 根据得到XML结构,逐一检查目标XML中是否存在指定的元素或属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值