2017-04-13 122 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