2012-02-21 98 views
1

今天我碰到以下问题。我有以下XML: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> 

而且它验证国际泳联针对它的架构。但我不希望在我的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以使实例文档有效?

回答

2

鉴于您所写的内容,我认为问题如下。

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

下面这第一个例子是有效的,因为a不合格因为你设置elementFormDefaultunqualified

第一个例子

<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> 

EDIT

如果根元素的子元素中相同的命名空间比被定义根在你的模式,你只需要更改elementFormDefault="unqualified"elementFormDefault="qualified"到哈有一个验证XML的模式。如果不是这样的话:你肯定必须更深入地重塑你的模式,在这种情况下,也许你应该用更多的代码(包括更多的模式和实例的一部分)发布另一个致力于该模式的问题。

+0

是的,我已经理解了这个问题。现在的问题是如何更改我的XSD以使我的实例文档在没有附加名称空间声明的情况下有效? – execc 2012-02-21 19:13:46

0

看起来您错误地认为您的根<docschema>中的嵌套元素将继承在该根上定义的名称空间。他们不会。

如果你想摆脱命名空间前缀,你将不得不在显示的实例文档中的每个子节点上声明命名空间。

<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> 

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

+0

问你答案。我看过这篇文章:http://www.xfront.com/HideVersusExpose.html,这里是隐藏命名空间的例子。这(只有根元素的前缀和名称空间声明)工作正常,我想要的只是使用默认前缀(例如没有前缀),因为我的元素不会与名称空间发生冲突。 – execc 2012-02-21 13:51:39

+0

对不起我的错误 - 我没有看到你的elementFormDefault设置。像这样使用XSD是不常见的。但是,您发布的链接样本仍在根节点中使用前缀。我不认为你可以做你想做的事,但我无法解释为什么 – 2012-02-21 14:44:04