【问题标题】:Is this a bug in XSD implementation? Why doesn't this work?这是 XSD 实现中的错误吗?为什么这不起作用?
【发布时间】: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 不起作用?

亲切的问候, 纪尧姆·哈尼克

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    在您的“备选方案 1”中,您引用的是“ExceptionType” - 但它并未在该文件中的任何位置声明。

    仅仅因为两个文件共享相同的命名空间并不意味着文件 A 可以依赖于文件 B 中的内容 - 您需要将两者连接起来!

    &lt;xsd:include&gt; 添加到您的第二个文件:

    <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:include schemaLocation="exceptiontype.xsd"/>
    
      <xs:element name="SpecificException" type="ExceptionType" />
    </xs:schema>
    

    这应该可以解决问题!

    马克

    【讨论】:

      猜你喜欢
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 2021-09-08
      相关资源
      最近更新 更多