Schema的名称空间解说
Schema的名称空间解说
Canca
昨天是个大日子,昨天我看了一个好贴子,在里面我找到了我的问题:Schema的名称空间。现在我就说说自己的心得,也就是自己对名称空间的理解吧!
Schema的targetNamespace是定义Schema里面的元素、属性、自定义类型的名称空间。那一定有人会问:有了targetNamespace定义了Schema的名称空间了,为什么还要声明xmlns呢?那不是多余的吗?其实,xmlns的主要作用,并不是你想像的主要为了声明Schema里面的元素、属性、自定义类型的名称空间。它的目的是为了Schema里引用自定义类型或自定义属性时可以缺省名称空间前缀,而直接引用元素名。请看下面例子:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.canca.org" xmlns ="http://www.canca.org" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element style="COLOR: yellow"< type=”stuType” maxOccurs=”unBounded” /> <xs:complexType < <xs:sequence> <xs:element type=”xs:string” /> <xs:element type=”xs:float” /> </xs:sequence> </xs:complexType> </xs:schema>
看看红色部分type=”stuType” 引用了自定义的复制类型,type=”stuType” 的意思是引用默认名称空间下名叫stuType的元素。而默认的名称空间(蓝色处)已定义为http://www.canca.org 这个了。因为stuType 是放在targetNamespace这个名称空间里面的。所以默认的名称空间要跟targetNamespace这个名称空间一样。否则也会出错。以上的例子的默认名称空间定义只是为了方便使用而已,其实它完全可以像以下这样写:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.canca.org" xmlns:my ="http://www.canca.org" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element style="COLOR: yellow"< type=” my:stuType” maxOccurs=”unBounded” /> <xs:complexType < <xs:sequence> <xs:element type=”xs:string” /> <xs:element type=”xs:float” /> </xs:sequence> </xs:complexType> </xs:schema>
上面没有定义默认名称空间,而定义一个用前缀名为my的与targetNamespace一样的名称空间。在引用类型时,加上前缀就可以了。
好了,现在说说另一个问题:Schema的elementFormDefault与attributeFormDefault有什么作用?
昨天看的贴子有说这个问题,但好像有点模糊,还有点错误呢,现在说说我的理解。我已经做过实验,验证了自己的理解了,大家放心。
ElementFormDefault与attributeFormDefault属性各有两个属性值:qualified、unqualified。如果elementFormDefault = “unqualified” ,即Schema除根元素以外的元素就没有名称空间,即xmlns=””。否则Schema中所有声明的元素或自定义类型都以targetNamespace所定义的作为名称空间。AttributeFormDefault与ElementFormDefault道理一样。呵呵,是不是很简单。现在看个例子就知道了。
<!—Test.xsd--!> <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.canca.org" xmlns="http://www.canca.org" elementFormDefault="unqualified" attributeFormDefault="unqualified"> <xs:element name="student" type="Student" /> <xs:complexType name="Student"> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="number" type="numberType" /> </xs:sequence> </xs:complexType> <xs:simpleType name="numberType"> <xs:restriction base="xs:string"> <xs:pattern value="\d{1}-\d{3}" /> </xs:restriction> </xs:simpleType> </xs:schema>
红色的为根元素下的子元素。以下看看XML的定义:
<?xml version="1.0" encoding="UTF-8"?> <student xmlns="http://www.canca.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.canca.org Test.xsd"> <name xmlns="">CAnca</name> <number xmlns="">1-123</number> </student>
因为Schema里将elementFormDefault设成 unqualified (elementFormDefault="unqualified") ,因此根元素以外的子元素没有名称空间。即使Schema文件设置了默认的名称空间也是一样。所以Schema实例里定义其元素时要将名称空间设成空。而父子素的名称空间即用Schema中的targetNamespace中定义的。好,现在看看另外的一种情况:
<!—Test.xsd--!> <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.canca.org" xmlns="http://www.canca.org" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="student" type="Student" /> <xs:complexType name="Student"> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="number" type="numberType" /> </xs:sequence> </xs:complexType> <<>xs:simpleType name="numberType"> <<>xs:restriction base="xs:string"> <<>xs:pattern value="\d{1}-\d{3}" /> <<>/xs:restriction> <<>/xs:simpleType> </xs:schema>
红色的为根元素下的子元素。以下看看XML的定义:
<?xml version="1.0" encoding="UTF-8"?> <student xmlns="http://www.canca.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.canca.org Test.xsd"> <name>CAnca</name> <number>1-123</number> </student>
因为Schema里将elementFormDefault设成 qualified (elementFormDefault="qualified") ,因此Schema中声明的所有元素或自定义类型都以targetNamespace作为名称空间。因为上例中的XML文档中的默认名称空间正是targetNamespace所定义的。因此,在声明子元素时无需声明其名称空间。而直接写元素名。如:<<>name>CAnca<<>/name>。
好了,终于写完了,这是我在这周里所体会的。现在在这里画上了个句号。希望此文对所有XML Schema初学者有所帮助。
CAnca Software Office
2007-3-30