【问题标题】:xsd import from xsd include从 xsd 导入 xsd 包括
【发布时间】:2019-11-05 11:23:39
【问题描述】:

我有以下“imp.xsd”:

<xsd:schema targetNamespace="http://imported"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Imported" type="xsd:string"/>
</xsd:schema>

从“incl.xsd”导入

<xsd:schema targetNamespace="http://main"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:import namespace="http://imported" schemaLocation="file:///C:/.../imp.xsd"/>
    <xsd:element name="Included" type="xsd:string"/>
</xsd:schema>

包含来自“main.xsd”:

<xsd:schema targetNamespace="http://main"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:include schemaLocation="file:///C:/.../incl.xsd"/>
    <xsd:complexType name="dummy">
        <xsd:sequence xmlns:impt="http://imported" xmlns:incl="http://main">
            <xsd:element ref="incl:Included"/>
            <xsd:element ref="impt:Imported"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

问题:“main.xsd”是一个有效的 xml 架构文档吗?

当使用 xmllint 解析器(或来自 python lxml 库,而后者又使用 xmllint)进行验证时,我收到以下错误:

元素'{http://www.w3.org/2001/XMLSchema}元素',属性'ref': 从此模式中引用命名空间中的组件 'http://imported' 是不允许的,因为导入时没有指明 陈述。 WXS 架构 main.xsd 编译失败

但是,另一个 (IBM) 解析器接受此 XSD 时没有任何投诉。

据我了解 xsd:include,它应该表现得好像整个包含的 XSD 都内联到主文档中。所以,在我看来,“main.xsd”应该是有效的。是吗?

编辑:我正在尝试xmllint,当我添加该行时

<xsd:import namespace="http://imported" schemaLocation="file:///C:/.../imp.xsd"/>

进入“main.xsd”,xmllint 接受该模式。但是,当我在省略 file:/// 的同时添加同一行时,会收到以下警告:

元素“{http://www.w3.org/2001/XMLSchema}import”:跳过导入 位于命名空间的“C:/.../imported.xsd”的架构 'http://imported',因为这个命名空间已经用 架构位于 'file:///V:/.../imported.xsd'。

因此我推断原始的“main.xsd”是有效的,这是xmllint 中的一个错误。

【问题讨论】:

    标签: xsd lxml libxml2 xmllint xsd-1.1


    【解决方案1】:

    请参阅 xmlschema-dev@w3.org 邮件列表中的 this thread。在他的回答中,Henry S. Thompson 引用了the spec 的部分内容:

    对于一个·QName· 来解析为一个模式组件 [...] ·QName·的·命名空间名称·与下列之一相同:

    [...]

    • 某些的命名空间[属性]的·实际值· 元素中包含的元素信息项 该架构文档的信息项 [强调添加]。

    所以间接导入不应该起作用。

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 2011-07-16
      • 2012-08-20
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      相关资源
      最近更新 更多