以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  请教xsd使用中namespace的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=69920)


--  作者:goukili
--  发布时间:11/28/2008 12:15:00 PM

--  请教xsd使用中namespace的问题
一共4个文件,分别用了两种方式定义schema,两个xml内容和结构是相同。

Demo1.xml, Demo1.xsd - 按照元素和属性定义结构
Demo2.xml, Demo2.xsd - 按照类型和类定义结构

[Demo1.xml]
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="Demo1"
      xmlns:a="Demo1"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="Demo1 Demo1.xsd"
      a:id="001"
      >
    <item a:id="001"></item>
    <item a:id="002"></item>
</root>

[Demo1.xsd]
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    targetNamespace="Demo1"
    elementFormDefault="qualified"
    xmlns="XSDDemo"
    xmlns:D="Demo1"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>    
    <xs:attribute name="id">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[0-9]{3}"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>

    <xs:element name="item">
        <xs:complexType>
            <xs:attribute ref="D:id"/>
        </xs:complexType>
    </xs:element>
    
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="D:item" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute ref="D:id"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

[Demo2.xml]
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="Demo2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="Demo2 Demo2.xsd"
      id="001"
      >
    <item id="001"/>
    <item id="002"/>
</root>

[Demo2.xsd]
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    targetNamespace="Demo2"
    elementFormDefault="qualified"
    xmlns="XSDDemo"
    xmlns:D="Demo2"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:simpleType name="sringType">
        <xs:restriction base="xs:string"></xs:restriction>
    </xs:simpleType>
    
    <xs:simpleType name="idType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9]{3}"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="itemType">
        <xs:attribute name="id" type="D:idType"/>
    </xs:complexType>
    
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element name="item" type="D:itemType" maxOccurs="unbounded"></xs:element>
        </xs:sequence>
        <xs:attribute name="id" type="D:idType"/>
    </xs:complexType>

    <xs:element name="root" type="D:rootType"/>
</xs:schema>

问题:
在Demo1.xml中,为什么一定要多定义一个xmlns:a="Demo1"之后,才能在root和item中写a:id="001"属性?而Demo2.xml中并不需要这样定义,就能直接写id="001"。两个xsd定义的结构是一样的。

请教大家,谢谢。


[此贴子已经被作者于2008-11-28 13:16:39编辑过]

--  作者:hexun831012
--  发布时间:11/28/2008 7:13:00 PM

--  
attributeFormDefault="qualified"
--  作者:goukili
--  发布时间:12/1/2008 10:39:00 AM

--  
以下是引用hexun831012在2008-11-28 19:13:00的发言:
attributeFormDefault="qualified"

我是想知道为什么两者用法会不同?为什么Demo2.xml用的时候不需要加prefix?

我的个人理解Demo1.xsd中的attribute是全局属性,又因为指定了elementFormDefault="qualified",所以全局元素和属性是在targetnamespace中的,所以使用的时候需要指定命名空间。发现Demo_1.xml也可以写成下面这样,因为所有的元素和属性都在targetnamespace中,所以都加上prefix也是对的,这样理解是否正确?

问题: 为什么之前的Demo1.xsd中的各节点不加prefix也可以,仅仅只需对其id属性加prefix?

修改后的Demo_1.xml也正确
<a:root xmlns="Demo1"
      xmlns:a="Demo1"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="Demo1 Demo1.xsd"
      a:id="001"
      >
    <a:item a:id="001"></a:item>
    <a:item a:id="002"></a:item>
</a:root>


--  作者:goukili
--  发布时间:12/1/2008 11:04:00 AM

--  
以下是引用hexun831012在2008-11-28 19:13:00的发言:
attributeFormDefault="qualified"

我又试了一下,重新改了Demo2.xsd,加上 attributeFormDefault="qualified"。说明所有属性也都在targetnamespace中,所以编写Demo2.xml的时候就需要指定prefix了(即和Demo1.xml一样使用)。

问题1:也就是说,对于Demo2.xsd,原先没有 attributeFormDefault="qualified"这句话的时候,属性是在defaultnamespace中的,有了这句话,就在targetnamesapce中了。这样理解是否正确??

问题2:既然是因为 attributeFormDefault="qualified"这句话的关系,那么为什么 Demo1.xsd中并没有这句定义,在使用的时候还是需要用prefix呢?实在是想不通!

问题3:elementFormDefault 和 attributeFormDefault 如果不指定的话,xsd文件是否会指定默认值??是什么??


--  作者:goukili
--  发布时间:12/2/2008 10:32:00 AM

--  
没有人么?
--  作者:goukili
--  发布时间:12/2/2008 1:59:00 PM

--  
斑竹人呢?请帮忙,谢谢。
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
68.359ms