您发布的 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。“tell-tale”是只有文档元素是合格的(在你的情况下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>