【发布时间】:2009-08-27 09:39:51
【问题描述】:
我有一个不想修改的 xsd 文件 (Exceptions.xsd):
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://me.com/Exceptions.xsd"
targetNamespace="http://me.com/Exceptions.xsd"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
>
<xs:element name="Exception" type="ExceptionType" />
<xs:complexType name="ExceptionType">
<xs:sequence>
<xs:element name="Code" type="xs:string" minOccurs="0"/>
<xs:element name="Message" type="xs:string"/>
<xs:element name="TimeStamp" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
我想创建一个具有其他名称的新元素,以实现该 ExceptionType(ExceptionsExtensions.xsd - 备选方案 1)。
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://me.com/Exceptions.xsd"
targetNamespace="http://me.com/Exceptions.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"
http://me.com/Exceptions.xsd Exceptions.xsd
"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="SpecificException" type="ExceptionType" />
</xs:schema>
我收到错误消息:未声明类型“http://me.com/Exceptions.xsd:ExceptionType”。
但是,如果我这样做(ExceptionExtensions.xsd - 备选方案 2):
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://me.com/Exceptions.xsd"
targetNamespace="http://me.com/Exceptions.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"
http://me.com/Exceptions.xsd Exceptions.xsd
"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="SpecificException">
<xs:complexType>
<xs:sequence>
<xs:element name="innerException">
<xs:complexType>
<xs:sequence>
<xs:any namespace="http://me.com/Exceptions.xsd" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
我可以验证
<?xml version="1.0" encoding="utf-8"?>
<SpecificException xmlns="http://me.com/Exceptions.xsd">
<innerException>
<Exception>
<Code>12</Code>
<Message>Message</Message>
<TimeStamp>2009-08-27T11:30:00</TimeStamp>
</Exception>
</innerException>
</SpecificException>
因此,在备选方案 1 中,它无法找到在 Exceptions.xsd 中声明的 ExceptionType,但在备选方案 2 中,它可以找到在 Exceptions.xsd 中声明的异常元素。
为什么备选方案 1 不起作用?
亲切的问候, 纪尧姆·哈尼克
【问题讨论】: