[XML]为什么不能再xml中使用默认命名空间呢

yjl491 2014-02-05 07:52:55
1)下面是xsd文件,保存成test.xsd 放置到:C:\temp下

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com" targetNamespace="http://www.test.com">

<xs:element name="purchaseOrder" type="PurchaseOrderType"/>
<xs:element name="comment" type="xs:string"/>

<xs:complexType name="PurchaseOrderType">
<xs:sequence>
<xs:element name="shipTo" type="USAddress"/>
<xs:element ref="comment" minOccurs="1"/>
<xs:element name="items" type="Items"/>
</xs:sequence>
<xs:attribute name="orderDate" type="xs:date" use="required"/>
</xs:complexType>

<xs:complexType name="USAddress">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="country" type="xs:NMTOKEN" fixed="US"/>
</xs:complexType>

<xs:complexType name="Items">
<xs:sequence>
<xs:element name="item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>

<xs:element name="productName" type="xs:string"/>
<xs:element name="quantity">
<xs:simpleType>
<xs:restriction base="xs:positiveInteger">
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="USPrice" type="xs:decimal"/>
<xs:element ref="comment" minOccurs="0"/>
<xs:element name="shipDate" type="xs:date" minOccurs="0"/>

</xs:sequence>
<xs:attribute name="partNum" type="SKU" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

<!--Stock Keep Unit,a code foridentifying pr ducts-->
<xs:simpleType name="SKU">
<xs:restriction base="xs:integer">
<xs:minInclusive value="2"/>
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>


下面是合法的XML文件,保存成test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<MY:purchaseOrder xmlns:MY="http://www.test.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://www.test.com C:/temp/test.xsd" orderDate="2010-02-11">

<shipTo country="US">
<name/>
<street/>
<city/>
<state/>
<zip>12.3</zip>
</shipTo>

<MY:comment/>

<items>
<item partNum="6">
<productName/>
<quantity>39</quantity>
<USPrice>56.9</USPrice>
<MY:comment>abc</MY:comment>
<shipDate>2012-03-11</shipDate>
</item>
</items>

</MY:purchaseOrder>

在XMLSpy里运行一下该XML文件,应该没问题。
但是,当把该XML文件改成默认命名空间的是后,就会报错,改成默认命名空间的代码如下:


<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrder xmlns="http://www.test.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://www.test.com C:/temp/test.xsd" orderDate="2010-02-11">

<shipTo country="US">
<name/>
<street/>
<city/>
<state/>
<zip>12.3</zip>
</shipTo>


<comment/>

<items>
<item partNum="6">
<productName/>
<quantity>39</quantity>
<USPrice>56.9</USPrice>
<comment>abc</comment>
<shipDate>2012-03-11</shipDate>
</item>
</items>

</purchaseOrder>


请帮忙分下一下,使用默认命名空间,代码该如何写?
...全文
4799 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cds27 2014-02-11
  • 打赏
  • 举报
回复
报什么错误?
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧