在本地为元素分配命名空间

时间:2013-04-17 14:25:27

标签: xml xsd

我有一个xml文件。我无法更改xml结构,因此我必须更改xsd文件。问题是关于命名空间

<po:purchaseOrder orderDate="2001-01-01" xmlns:po="http://objectshop.com/ns/po" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://objectshop.com/ns/po po.xsd ">
<shipTo country="USA">
    <name>Alice Smith</name>
    <street>123 Maple Street</street>
    <city>Cambridge</city>
    <state>MA</state>
    <postalcode>12345</postalcode>
</shipTo>
</po:purchaseOrder>

所以通常我应该在每个元素中添加ns。如果我这样做就没事了。我如何设计xsd文件的问题。特别是如何在xsd?

中本地分配命名空间
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://objectshop.com/ns/po"
elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:po="http://objectshop.com/ns/po">

<complexType name="PurchaseOrderType">
    <sequence>
        <element name="shipTo" type="po:Address" maxOccurs="unbounded" minOccurs="0" ></element>
        <element name="billTo" type="po:Address" maxOccurs="unbounded" minOccurs="0"></element>
        <element name="items" type="po:Items" maxOccurs="unbounded" minOccurs="0"></element>
    </sequence>

    <attribute name="orderDate" type="date"></attribute>
</complexType>
</schema>

1 个答案:

答案 0 :(得分:1)

您发布的XSD不完整,因此很难说您的设置是什么。所以我将基于XML解释两个不同的选项。一个人应该考虑你的具体情况。

如果我从XML开始并从中生成XSD,那么这就是您通常会得到的:

<强> XSD1:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns:po="http://objectshop.com/ns/po" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://objectshop.com/ns/po" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:import schemaLocation="XSD2.xsd" />
  <xsd:element name="purchaseOrder">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="shipTo" />
      </xsd:sequence>
      <xsd:attribute name="orderDate" type="xsd:date" use="required" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

<强> XSD2:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="shipTo">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="name" type="xsd:string" />
        <xsd:element name="street" type="xsd:string" />
        <xsd:element name="city" type="xsd:string" />
        <xsd:element name="state" type="xsd:string" />
        <xsd:element name="postalcode" type="xsd:unsignedShort" />
      </xsd:sequence>
      <xsd:attribute name="country" type="xsd:string" use="required" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

这两个XSD验证您发布的XML。他们展示的是如何引用没有命名空间的内容 - 这就是我认为你的问题所在。

但是,也可以使用一个XSD文件描述的XML。 “告诉”是指只有文档元素是合格的(在您的情况下为purchaseOrder),而其他所有内容都是“不合格的”。要在默认情况下实现此目的,需要使用elementFormDefault="unqualified"设置架构。

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns:po="http://objectshop.com/ns/po" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://objectshop.com/ns/po" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="purchaseOrder" type="po:PurchaseOrderType"/>
    <xsd:complexType name="PurchaseOrderType">
        <xsd:sequence>
            <xsd:element name="shipTo" type="po:Address" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
        <xsd:attribute name="orderDate" type="xsd:date" use="required"/>
    </xsd:complexType>
    <xsd:complexType name="Address">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="street" type="xsd:string"/>
            <xsd:element name="city" type="xsd:string"/>
            <xsd:element name="state" type="xsd:string"/>
            <xsd:element name="postalcode" type="xsd:unsignedShort"/>
        </xsd:sequence>
        <xsd:attribute name="country" type="xsd:string" use="required"/>
    </xsd:complexType>
</xsd:schema>

或者,对于每个元素/属性,您可以通过设置表单属性来覆盖模式级别的默认设置。下面是一个示例(仅用于说明,并不意味着与您的XML匹配)。

<xsd:element name="name" type="xsd:string" form="qualified"/>
<xsd:attribute name="country" type="xsd:string" use="required" form="qualified"/>

在这种情况下,XML应为:

<po:purchaseOrder orderDate="2001-01-01" xmlns:po="http://objectshop.com/ns/po" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://objectshop.com/ns/po po.xsd ">
    <shipTo po:country="USA">
        <po:name>Alice Smith</po:name>
        <street>123 Maple Street</street>
        <city>Cambridge</city>
        <state>MA</state>
        <postalcode>12345</postalcode>
    </shipTo>
</po:purchaseOrder> 
相关问题