XML Schema--<schema>标签的elementFormDefault属性的作用

如果想了解更多,可以参考http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/#PO

1.     全局元素和局部元素

在讨论elementFormDefault属性之前,先让我们认识一下在一个xsd文件中,什么元素称为全局元素,什么元素称为局部元素

示例:

[html]  view plain copy
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  2.   
  3.        <xs:element name="purchaseOrder" type="PurchaseOrderType"/>   
  4.        <xs:element namexs:elementname="comment" type="xs:string"/>  
  5.   
  6.        <xs:complexType name="PurchaseOrderType">  
  7.               <xs:sequence>  
  8.                      <xs:element name="shipTo" type="xs:string"/>  
  9.                      <xs:element name="billTo" type="xs:string"/>  
  10.                      <xs:element ref="comment" minOccurs="0"/>  
  11.              </xs:sequence>  
  12.        </xs:complexType>  
  13.   
  14. </xs:schema>  

此例中,第3,4,6行是全局元素声明,8,9行是局部元素。可以这么说,<schema>的直接子元素称为全局元素,嵌套在其他元素中的元素称为局部元素。 

2.     elementFormDefault属性的值与作用域

值:

elementFormDefault=”qualified” 或者elementFormDefault=”unqualified”

作用域:

只对局部元素起作用,也就是说:不管elementFormDefault为何值,如果指定了<schema>标签属性targetNamespace的值,所有全局元素都将归于该targetNamespace所指定的命名空间。如果elementFormDefault="qualified",所有的局部元素也都归于targetNamespace指定的命名空间,如果elementFormDefault="unqualified",则局部元素没有命名空间


elmentFormDefault

全局元素

局部元素

qualified

属于<schema>标签属性targetNamespace指定的命名空间,如果对该命名空间指定了前缀,则使用元素时,必须加前缀

属于<schema>标签属性targetNamespace指定的命名空间,如果对该命名空间指定了前缀,则使用元素时,必须加前缀

unqualified

属于<schema>标签属性targetNamespace指定的命名空间,如果对该命名空间指定了前缀,则使用元素时,必须加前缀

无命名空间,使用元素时,无需任何前缀

我们看下面的一个例子po1.xsd

[html]  view plain copy
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
  2.           xmlns:po="http://www.example.com/PO1"  
  3.           targetNamespace="http://www.example.com/PO1"  
  4.           elementFormDefault=" qualified ">  
  5.  
  6.     <xs:element name="purchaseOrder" type="po:PurchaseOrderType"/>  
  7.     <xs:element name="comment" type="xs:string"/>  
  8.   
  9.     <xs:complexType name="PurchaseOrderType">  
  10.         <xs:sequence>  
  11.             <xs:element name="shipTo" type="po:USAAddress"/>  
  12.             <xs:element name="billTo" type="po:USAAddress"/>  
  13.             <xs:element ref="po:comment" minOccurs="0"/>  
  14.         </xs:sequence>  
  15.     </xs:complexType>  
  16.   
  17.     <xs:complexType name="USAAddress">  
  18.         <xs:sequence>  
  19.             <xs:element name="name" type="xs:string"/>  
  20.             <xs:element name="street" type="xs:string"/>  
  21.         </xs:sequence>  
  22.     </xs:complexType>  
  23.   
  24. </xs:schema>  

在此xsd文件中,我们定义了(注意加了前缀的引用该xsd文件的全局元素。在<schema>中你对targetNamespace指定的命名空间指定了前缀po)

targetNamespace=” http://www.example.com/PO1”  elementFormDefualt=” qualified”

对应的xml实例po1.xml

[html]  view plain copy
  1. <?xml version="1.0"?>  
  2. <apo:purchaseOrder xmlns:apo="http://www.example.com/PO1"  
  3.                    orderDate="2012-02-09">  
  4.     <apo:shipTo>  
  5.         <apo:name>Alice Smith</apo:name>  
  6.         <apo:street>123 Maple Street</apo:street>  
  7.     </apo:shipTo>  
  8.     <apo:billTo>  
  9.         <apo:name>Robert Smith</apo:name>  
  10.         <apo:street>8 Oak Avenue</apo:street>  
  11.     </apo:billTo>  
  12.     <apo:comment>Hurry, my lawn is going wild</apo:comment>  
  13. </apo:purchaseOrder></span>  

 (注意到无论是全局元素的使用还是局部元素的使用都加了前缀,前缀apo是通过xmlns:apo=”http://www.example.com/PO1”指定的)

再来看一个例子po2.xsd(此例修改po1.xsd中的elementFormDefault=” unqualified”)

[html]  view plain copy
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
  2.                xmlns:po="http://www.example.com/PO1"  
  3.                targetNamespace="http://www.example.com/PO1"  
  4.                elementFormDefault=" qualified ">   
  5.   
  6.       <xs:element name="purchaseOrder" type="po:PurchaseOrderType"/>  
  7.       <xs:element name="comment" type="xs:string"/>  
  8.    
  9.       <xs:complexType name="PurchaseOrderType">  
  10.              <xs:sequence>  
  11.                     <xs:element name="shipTo" type="po:USAAddress"/>  
  12.                     <xs:element name="billTo" type="po:USAAddress"/>  
  13.                     <xs:element ref="po:commentminOccurs="0"/>  
  14.             </xs:sequence>  
  15.       </xs:complexType>  
  16.    
  17.       <xs:complexType name="USAAddress">  
  18.              <xs:sequence>  
  19.                     <xs:element name="name" type="xs:string"/>  
  20.                     <xs:element name="street" type="xs:string"/>  
  21.              </xs:sequence>  
  22.       </xs:complexType>  
  23.   
  24. </xs:schema></span>  

  对应的xml实例po2.xml 

[html]  view plain copy
  1. <?xml version="1.0"?>  
  2. <apo:purchaseOrder xmlns:apo="http://www.example.com/PO1"  
  3.                    orderDate="2012-02-09">  
  4.     <shipTo>  
  5.         <name>Alice Smith</name>  
  6.         <street>123 Maple Street</street>  
  7.     </shipTo>  
  8.     <billTo>  
  9.         <name>Robert Smith</name>  
  10.         <street>8 Oak Avenue</street>  
  11.     </billTo>  
  12.     <apo:comment>Hurry, my lawn is going wild</apo:comment>  
  13. </apo:purchaseOrder></span>  

(注意到只对xsd文件中的全局元素的使用加了前缀,前缀apo是通过xmlns:apo=” http://www.example.com/PO1”指定的) 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值