2017-04-13 121 views
0

我有一个xml,我想验证一个xsd(与多个模式)。 XML是如下:xml没有验证没有命名空间前缀

<?xml version="1.0" encoding="utf-8"?> 
<Interchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.abc.com.au/eApplications/Interchange/4.2"> 
    <InterchangeID>19768</InterchangeID>......</Interchange> 

这个XML没有通过验证,并给了我以下错误:

The 'http://www.abc.com.au/eApplications/Interchange/4.2:Interchange' element is not declared. 

当我介绍了命名空间,像这样的xml:

xmlns:ns="http://www.abc.com.au/eApplications/Interchange/4.2" 

错误消失了,即使我没有引入像名称空间前缀一样的XML标签或任何类似的标签:

<ns:Interchange> 
    <ns:InterchangeID>... 

我的发现: 的XML通过验证,如果我任一介绍:纳秒为名称空间或包含所有定义的命名空间引用完全除去线(父交换标签之后),并保持这样的:

​​

这显然不好。

虽然我能够用该方法来解决这个问题上面解释的,我想知道这背后的原因,因为我无法经过许多环节thisA Ten-Minute Guide to XML NamespacesXML Namespaces Explained

去编辑 后发现我在这里的附加XSD:

enter code here 
<xs:schema xmlns:eapp="http://www.abc.com.au/eApps/iChange/4.2" 
    elementFormDefault="qualified" 
     targetNamespace="http://www.abc.com.au/eApps/iChange/4.2" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

     <xs:include schemaLocation="abc_datatypes.xsd"/> 
     <xs:complexType name="abcType"> 
     <xs:sequence> </xs:sequence> 
    </xs:complexType> 
    </xs:schema> 

    and the xml sample that passes validation is something like: 


<soapenv:Envelope xmlns:soapenv="url" 
    xmlns:orig="url/OServices" xmlns:ns="http://url/eApps/IChange/4.2"> 
    <soapenv:Header/> 
    <soapenv:Body> 
    <or:Originate> 
     <!--Optional:--> 
     <or:data> 
     <ns:InID>2229</ns:InID> 
     <ns:ReceiverID>CFS</ns:ReceiverID> 
    </or:data> 
    </or:Originate> 
    </soapenv:Body> 
</soapenv:Envelope> 
+0

“Interchange”元素的XSD定义是什么? – kennyzx

+0

寻求调试帮助的问题(“为什么这个代码不工作?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。请参阅:如何创建[mcve]。 – kjhughes

+0

当您删除命名空间前缀时,根节点只会变成一个完全未知的节点 - 而未知节点则被视为警告,而不是错误。请参阅[XDocument.Validate始终成功](http://stackoverflow.com/q/17232575/3744182)。通常,任何全局定义的元素都可以是根元素,请参阅[XML Schema:根元素](http://stackoverflow.com/q/8854144/3744182),这可能是为什么这是警告而不是错误。 – dbc

回答

0

TL;博士

My Findings: The xml passes the validation if I either introduce :ns as a namespace or remove the line completely (after the parent Interchange tag) containing all the namespace references defined and keep it like that:

<Interchange> 
    <InterchangeId>.... 

which is obviously not good.

它通过了验证,因为这两个方法都删除了默认名称空间。

附加说明...

This xml didn't pass the validation and was giving me the following error:

The 'http://www.abc.com.au/eApplications/Interchange/4.2:Interchange' element is not declared. 

您没有提供架构,所以我们无法验证,但是这是什么要说的是,在Interchange元素的``命名空间中不存在的架构。

当您声明一个没有前缀(xmlns="some uri")的名称空间时,它将成为该上下文的默认名称空间。

既然你说没有任何验证错误,当你完全删除xmlns="http://www.abc.com.au/eApplications/Interchange/4.2",这导致我相信Interchange不在架构中的命名空间。

When I introduced namespace with xml like this:

xmlns:ns="http://www.abc.com.au/eApplications/Interchange/4.2" 

The error was gone,...

那是因为你绑定的命名空间URI http://www.abc.com.au/eApplications/Interchange/4.2到前缀ns,现在还没有一个默认的命名空间。

+0

刚刚更新了xsd和示例xml – sam

+0

我确信我有正确的xml内置,但可能我通过传递错误的启动标记(如'交换' ),因为这是通过使用随xsd提供的.cs自动生成的。 – sam