一个简单的 XML 文档:请看这个名为 "note.xml" 的 XML 文档:
<?xml version="1.0"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
XML Schema下面这个例子是一个名为 "note.xsd" 的 XML Schema 文件,它定义了上面那个 XML 文档的元素:
<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.w3school.com.cn"xmlns="http://www.w3school.com.cn"elementFormDefault="qualified"><xs:element name="note"> <xs:complexType> <xs:sequence><xs:element name="to" type="xs:string"/><xs:element name="from" type="xs:string"/><xs:element name="heading" type="xs:string"/><xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element>
</xs:schema>提示:XML Schema是用来定义元素和类型的<schema> 元素是每一个 XML Schema 的根元素:<schema> 元素可包含属性。下面的片断:
xmlns:xs="http://www.w3.org/2001/XMLSchema"显示 schema 中用到的元素和数据类型来自命名空间 "http://www.w3.org/2001/XMLSchema"。同时它还规定了来自命名空间 "http://www.w3.org/2001/XMLSchema" 的元素和数据类型应该使用前缀 xs:
这个片断:
targetNamespace="http://www.w3school.com.cn" 显示被此 schema 定义的元素 (note, to, from, heading, body) 来自命名空间: "http://www.w3school.com.cn"。
这个片断:
xmlns="http://www.w3school.com.cn" 指出默认的命名空间是 "http://www.w3school.com.cn"。
这个片断:
elementFormDefault="qualified" 指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定。
xs表示QName中的prefixschema表示QName中的localPart,说明这是一个schema类型的文档http://www.w3.org/2001/XMLSchema表示QName中的namespaceURI
请问Schema中elementFormDefault="qualified"是起什么作用呀?
我看到有的xsd文件的开头是这样写的:<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.abc.com" xmlns:ps="http://www.abc.com" elementFormDefault="qualified">
可是去掉elementFormDefault="qualified"好象也没什么影响呀?
版主的回答:http://xml.org.cn/dispbbs.asp?BoardID=23&id=25672&replyID=17053&star=1&skin=0http://hi.baidu.com/bailiangcn/blog/item/0d7cf83d020c33ea3c6d9742.html
targetNamesapce的作用这一个帖子至少看了20次以上了.只能说是越来越明白.目前又有所体会不知道对不对.
targetnamespace是来限定schema中出现的元素(note,to,from,heading,body)和属性的名称空间.而elementFormDefault="qualified"是用来限定它将验证的XML文件是否使用targetnamespace指定的名称空间.]
动手试比光看要管用。
我就是试了之后才体会到上面两点的!我是这样试的
1)把下面qualified全修改为unqualified,没有什么反应.2)把schema子元素的po:前缀删了,文档 就变成无效的了
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:po="http://www.example.com/PO1" targetNamespace="http://www.example.com/PO1" elementFormDefault="qualified" attributeFormDefault="qualified">
<element name="purchaseOrder" type="po:PurchaseOrderType"/><element name="comment" type="string"/>
<complexType name="PurchaseOrderType"> <sequence> <element name="shipTo" type="po:USAddress"/> <element name="billTo" type="po:USAddress"/> <element ref="po:comment" minOccurs="0"/> <!-- etc. --> </sequence> <!-- etc. --></complexType>
<complexType name="USAddress"> <sequence> <element name="name" type="string"/> <element name="street" type="string"/> <!-- etc. --> </sequence></complexType>
<!-- etc. -->
</schema>
在schema里面是没有反应,有反应的是它所说明的xml实例文档。如果你把schema中元素声明中type属性值中的po:删掉,那这个schema本身就错了。例如,如果把:<element name="purchaseOrder" type="po:PurchaseOrderType"/>改成:<element name="purchaseOrder" type="PurchaseOrderType"/>那这个schema本身就不对了,因为找不到PurchaseOrderType这个类型。这是因为,这时 type="PurchaseOrderType"中PurchaseOrderType类型前缀为空,验证它的程序就认为它属于缺省命名空间,而此时缺 省命名空间是"http://www.w3.org/2001/XMLSchema"(由xmlns="http://www.w3.org/2001/XMLSchema"声明),但是这个命名空间里面没有PurchaseOrderType这个类型,所以出错。
Henry S. Thompson(XML Schema 的编委之一,供职于爱丁堡大学教授)在这里 http://lists.xml.org/archives/xml-dev/200007/msg00064.html 回答了对于 elementFormDefault 的作用质疑。依我的理解,可以简单认为 elementFormDefault 的存在是为了方便,并不是必需。
此外,XML Schema的复杂性引起了很多争议。其中一部分的复杂性就来自这些可有可无的东西,它们带来方便,也带来麻烦。
什么是简易元素?简易元素指那些仅包含文本的元素。它不会包含任何其他的元素或属性。
定义简易元素的语法:<xs:element name="xxx" type="yyy"/>例子:这是一些 XML 元素:
<lastname>Refsnes</lastname><age>36</age><dateborn>1970-03-27</dateborn>这是相应的简易元素定义:<xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/>
限定(restriction)用于为 XML 元素或者属性定义可接受的值。对 XML 元素的限定被称为 facet。对值的限定下面的例子定义了带有一个限定的名为 "age" 的元素。age 的值不能低于 0 或者高于 120:<xs:element name="age"><xs:simpleType><xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/></xs:restriction></xs:simpleType></xs:element> 上面的例子也可以写成<xs:simpleType name="MyAgeType"><xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/></xs:restriction></xs:simpleType><xs:element name="age" type="MyAgeType"/>在这种情况下,类型 "MyAgeType" 可被其他元素使用,因为它不是 "age" 元素的组成部分。
简易元素就讲完了,现在我们可以看懂wsdl:types中的第一行了看第一项<schema targetNamespace="http://apache.org/hello_world_soap_http/types" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://apache.org/hello_world_soap_http/types" elementFormDefault="qualified"> <simpleType name="MyStringType"> <restriction base="string"> <maxLength value="30"/> </restriction> </simpleType>其中targetNamespace定义了在本schema中所定义的元素或类型(MyStringType,sayHi,sayHiResponse...)的命名空间xmlns:tns定义了在本schema中所定义的元素或类型(MyStringType,sayHi,sayHiResponse...)在使用时必须用tns前缀
xmlns="http://www.w3.org/2001/XMLSchema"表示本schema中所使用的元素(simpeType,restriction,element..)使用的缺省命名空间
自定义名为MyStringType扩展自string的简易类型,对其做了最大长度为30的限定使用方法:<element name="requestType" type="tns:MyStringType"/>
什么是复合元素?复合元素指包含其他元素及/或属性的 XML 元素。复合元素的例子复合元素,"product",是空的:<product pid="1345"/>
复合元素,"employee",仅包含其他元素:<employee><firstname>John</firstname><lastname>Smith</lastname></employee>
复合元素,"food",仅包含文本:<food type="dessert">Ice cream</food>
复合元素,"description",包含元素和文本:<description>It happened on <date lang="norwegian">03.03.99</date> ....</description>
如何定义复合元素?请看这个复合 XML 元素,"employee",仅包含其他元素:
<employee><firstname>John</firstname><lastname>Smith</lastname></employee>
通过命名此元素,可直接对"employee"元素进行声明,就像这样:<xs:element name="employee"><xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType></xs:element>上面的例子也可以写成<xs:complexType name="personinfo"><xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/></xs:sequence></xs:complexType>
<xs:element name="employee" type="personinfo"/>
现在我们可以看懂wsdl:types中余下的部分了,截取前三个
1. 定义一个复合类型的元素<sayHi></sayHi>可以包含任何子元素<element name="sayHi"> <complexType/></element>
2. 定义一个复合类型的元素<sayHiResponse> <responseType></responseType></sayHiResponse>第一个子元素必须是responseType<element name="sayHiResponse"> <complexType> <sequence> <element name="responseType" type="string"/> </sequence> </complexType></element>
3. 定义一个复合类型的元素<greetMe>第一个子元素必须是requestType,类型是最大长度为30的字符串<element name="greetMe"> <complexType> <sequence> <element name="requestType" type="tns:MyStringType"/> </sequence> </complexType></element>
一共定义了sayHi,sayHiResponse,greetMe,greetMeResponse,greetMeOneWay,pingMe,pingMeResponse,faultDetail共个复合类型的元素这些元素刚好在八个message中用到了
<wsdl:types><schema targetNamespace="http://apache.org/hello_world_soap_http/types" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://apache.org/hello_world_soap_http/types" elementFormDefault="qualified"> <simpleType name="MyStringType"> <restriction base="string"> <maxLength value="30"/> </restriction> </simpleType> <element name="sayHi"> <complexType/> </element> <element name="sayHiResponse"> <complexType> <sequence> <element name="responseType" type="string"/> </sequence> </complexType> </element> <element name="greetMe"> <complexType> <sequence> <element name="requestType" type="tns:MyStringType"/> </sequence> </complexType> </element> <element name="greetMeResponse"> <complexType> <sequence> <element name="responseType" type="string"/> </sequence> </complexType> </element> <element name="greetMeOneWay"> <complexType> <sequence> <element name="requestType" type="string"/> </sequence> </complexType> </element> <element name="pingMe"> <complexType/> </element> <element name="pingMeResponse"> <complexType/> </element> <element name="faultDetail"> <complexType> <sequence> <element name="minor" type="short"/> <element name="major" type="short"/> </sequence> </complexType> </element></schema></wsdl:types>