【问题标题】:XML Namespaces and validationXML 命名空间和验证
【发布时间】:2012-02-21 12:32:45
【问题描述】:

今天我遇到了以下问题。我有以下 xml:

<c:docschema xmlns:c="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
   ...
</c:docschema>

它正在根据它的架构验证 fina。但我不想在我的 xml 中使用命名空间前缀,所以我尝试这样写:

<docschema xmlns="http://www.otr.ru/sufd/document/desc"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
       ...
</docschema>

它给了我一个验证错误。我正在验证的 XSD 架构是两个 XSD 的复合体,这里是标题:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified"
        elementFormDefault="unqualified"
        xmlns="http://www.otr.ru/sufd/document/desc"
        targetNamespace="http://www.otr.ru/sufd/document/desc"
        xmlns:fieldset="http://www.otr.ru/sufd/document/fieldset"
        xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">


<xsd:import namespace="http://www.otr.ru/sufd/document/fieldset" schemaLocation="fieldset.xsd"/>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified"
        elementFormDefault="unqualified"
        targetNamespace="http://www.otr.ru/sufd/document/fieldset"
        xmlns="http://www.otr.ru/sufd/document/fieldset">

怎么了?

编辑:现在的问题是,如何更改我的 XSD 以使实例文档有效?

【问题讨论】:

    标签: xsd xml-namespaces


    【解决方案1】:

    鉴于你写的,我想问题如下。

    让我们考虑在您的根元素下有一个a 元素。

    下面的第一个示例是有效的,因为a 是不合格的并且因为您将elementFormDefault 设置为unqualified

    第一个例子

    <c:docschema xmlns:c="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
       <a>...</a>
    </c:docschema>
    

    在第二个示例中,文件无效,因为您将 elementFormDefault 设置为 unqualified 并且您有一个合格的元素 a(在默认命名空间中):

    第二个例子

    <docschema xmlns="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
       <a>...</a>
    </docschema>
    

    正确的 XML 可能是:

    <docschema xmlns="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
       <a xmlns="">...</a>
    </docschema>
    

    编辑

    如果根元素的子元素定义在与架构中的根相同的命名空间中,您只需将 elementFormDefault="unqualified" 更改为 elementFormDefault="qualified" 即可拥有一个验证 XML 的架构。如果不是这样:您肯定必须更深入地重塑您的架构,在这种情况下,也许您应该发布另一个专门针对该问题的问题,其中包含更多代码(包括更多部分架构和实例)。

    【讨论】:

    • 是的,我明白了这个问题。现在的问题是如何更改我的 XSD 以使我的实例文档在没有额外命名空间声明的情况下有效?
    【解决方案2】:

    看起来您错误地假设根 &lt;docschema&gt; 内的嵌套元素将继承该根上定义的命名空间。他们不会。

    如果您想摆脱命名空间前缀,则必须在实例文档的每个子节点上显式声明命名空间。

    例如

    <Root xmlns="http://www.myns.com">
      <MyElement1 xmlns="http://www.myns.com">
      ... etc
      </MyElement1>
    </Root>
    

    <p:Root xmlns:p="http://www.myns.com">
      <p:MyElement1>
      ... etc
      </p:MyElement1>
    </p:Root>
    

    哪个更好?我认为是第二种选择。

    【讨论】:

    • 谢谢你的回答。我看过这篇文章:xfront.com/HideVersusExpose.html,这是一个隐藏命名空间的示例。那(在根元素中只有前缀和命名空间声明)工作正常,我想要的只是使用默认前缀(例如没有前缀),因为我的元素没有与命名空间发生冲突。
    • 对不起我的错误 - 我没有看到你的 elementFormDefault 设置。像这样使用 XSD 并不常见。但是,您发布的链接中的示例仍然在根节点中使用前缀。我不认为你可以做你想做的事,但我无法解释为什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2011-02-28
    • 1970-01-01
    相关资源
    最近更新 更多