xml中qualified的作用

elementFormDefault的意思是局部元素是否需要描述他的命名空间。当为unqualified的时候,就是局部的元素不需要声明他的命名空间,默认是使用父的命名空间。当为qualified的时候,不管是父元素还是子元素,都需要描述他的命名空间的。
当然,当为qualified的时候,我们也可以使用默认空间来省略命名空间的编写。但是属性就没有默认空间这一层概念了。

比如:
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test" elementFormDefault="unqualified" attributeFormDefault="unqualified">
	<xs:element name="学生">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="姓名" type="xs:string"/>
				<xs:element name="性别" type="xs:string"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
这个文件中指明了elementFormDefault为unqualified。因此在xml中,我们的子元素(姓名、性别)就不需要描述他的命名空间了:
<?xml version="1.0" encoding="UTF-8"?>
<test:学生 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:test="test"  xsi:schemaLocation="test test.xsd">
	<姓名>王二</姓名>
	<性别>男</性别>
</test:学生>
假如设置为qualified,那么子元素就需要了描述命名空间了:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="学生">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="姓名" type="xs:string"/>
				<xs:element name="性别" type="xs:string"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
以上的代码跟第一份的代码的区别就只是unqualified变为qualified,则XML文件需要为:
<?xml version="1.0" encoding="UTF-8"?>
<test:学生 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:test="test"  xsi:schemaLocation="test test.xsd">
	<test:姓名>王二</test:姓名>
	<test:性别>男</test:性别>
</test:学生>

当elementFromDefault为qualified的时候,我们还可以使用默认命名空间来简化代码:
<?xml version="1.0" encoding="UTF-8"?>
<学生 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="test"  xsi:schemaLocation="test test.xsd">
	<姓名>王二</姓名>
	<性别>男</性别>
</学生>

使用默认命名空间的时候xmlns:test="test"变为xmlns="test"。

attributeFromDefault跟elementFromDefault的作用是类似,不过前者是指属性,后者是指元素。而且属性是不适用于默认命名空间的机制的。



  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值