【问题标题】:XSD expects right element of wrong namespsaceXSD 期望错误命名空间的正确元素
【发布时间】:2014-10-17 13:24:23
【问题描述】:

我想在命名空间“foo”中定义一个模式,该模式在命名空间“bar”中导入一个定义了复杂类型的模式,并引用“bar”中的类型。我缺少什么来验证这个? MWE 下面。

根架构:

<?xml version="1.0" encoding="ISO-8859-1"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:f="foo" xmlns:b="bar" targetNamespace="foo">

<import schemaLocation="Import.xsd" namespace="bar"/>

<element name="root" type="f:Root"/>

<complexType name="Root">
    <sequence>
        <!--<element ref="b:imported"/>-->
        <element name="imported" type="b:ImportedType"/>
    </sequence>
</complexType>
</schema>

导入的架构:

<?xml version="1.0" encoding="ISO-8859-1"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="bar"
    elementFormDefault="qualified">
<complexType name="ImportedType"/>
<!--
<element name="imported">
    <complexType/>
</element>
-->
</schema>

XML 实例:

<?xml version="1.0" encoding="UTF-8"?>
<f:root xmlns:f="foo" xmlns:b="bar">
    <b:imported/>
</f:root>

结果: 元素“{bar}imported”:不需要此元素。预期为 ({foo}imported )。

如果我将设计模式从 Venetian Blind 更改为 Salami Slice(在模式中切换 cmets),一切正常。但是我们所有的其他模式都在 VB 中,所以我不希望针对这种情况进行更改。

尝试使用 xmllint 和 notepad++ 进行验证

【问题讨论】:

  • 所以你的目标是拥有b:imported 而不是f:imported?
  • 目标是让 XML 文档保持原样。我不能改变它的布局。我也无法更改导入模式的布局,因为它不是我们的。我试图理解为什么它认为“imported”应该属于命名空间 foo,而我已经将它的类型指定为“b:ImportedType”(bar)
  • 你有点误会了。 type 仍在 b 中,但您的元素 imported 未导入,它在您的架构中声明。
  • 是的,我刚刚意识到这一点,因为我已经发布了您评论的答案。因此,要在不同的命名空间中使用子元素,我是否必须 才能“突破”当前命名空间?
  • 我一直这么认为,但我还在架构规范中看到了targetNamespace 属性:w3.org/TR/xmlschema11-1/#dcl.elt.local 所以我会尝试写targetNamespace="bar",或者可能是一个名称b:imported。我手头没有IDE,你能试试吗?

标签: xml xsd


【解决方案1】:

您希望拥有b:imported 而不是f:imported

问题是,您已经导入并使用了一个类型。而您的类型ImportedType 仍在b 中。

但是,您的元素 imported 不是从 b 导入的(尽管名称如此),而是在 f 中声明。

因此f:imported 是正确且符合预期的。

如果您想用元素“转义”命名空间,请在 f 中声明 imported 并改用元素引用:

<xs:element ref="b:imported"/>

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2019-11-17
    • 1970-01-01
    相关资源
    最近更新 更多