Xsd XML名称空间和验证

Xsd XML名称空间和验证,xsd,xml-namespaces,Xsd,Xml Namespaces,今天我遇到了以下问题。我有以下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"> .

今天我遇到了以下问题。我有以下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以使实例文档有效?

看起来您犯了一个错误,认为根
中的嵌套元素将继承在该根上定义的命名空间。他们不会

如果想要去掉名称空间前缀,那么必须在实例文档中的每个子节点上显式声明名称空间

乙二醇


... 等


... 等

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

根据您所写的内容,我认为问题如下

让我们考虑在根元素下面有一个<代码>一个<代码>元素。< /p> 下面的第一个示例是有效的,因为

a
是不合格的,并且您将
elementFormDefault
设置为
不合格的

第一个示例

<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>
<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的模式。如果不是这样的话:你肯定必须更深入地重塑你的模式,在这种情况下,也许你应该用更多的代码(包括更多部分的模式和实例)发布另一个专门的问题。

谢谢你的回答。我看过这篇文章:,下面是一个隐藏名称空间的例子。这(根元素上只有前缀和名称空间声明)很好,我只想使用默认前缀(例如,没有前缀),因为我的元素没有与名称空间冲突。对不起,我的错误-我没有看到您的elementFormDefault设置。像这样使用XSD是不常见的。但是,您发布的链接中的示例仍在根节点中使用前缀。我不认为你能做你想做的事,但我不能确切地解释为什么是的,我理解这个问题。现在的问题是如何更改我的XSD,以便使我的实例文档在没有附加名称空间声明的情况下有效?
<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>
<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>
<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>