2012-09-12 22 views
3

我在SOAP響應驗證中遇到了一個奇怪的問題。我已經將響應和XSD降低到重現錯誤所需的最低限度。該XSD:XSD驗證期望它抱怨的元素

<?xml version="1.0"?> 
<xs:schema targetNamespace="http://peoplesoft.com/rootResponse" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="root"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element maxOccurs="unbounded" name="ReturnID" /> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

SOAP響應:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
    <root xmlns="http://peoplesoft.com/rootResponse"> 
     <ReturnID /> 
    </root> 
    </soapenv:Body> 
</soapenv:Envelope> 

如果我在驗證soapUI的原始響應,它說:預計元素 'ReturnID' 而不是「ReturnID @ HTTP://peoplesoft.com/rootResponse'元素root @ http://peoplesoft.com/rootResponse

當我在Visual Studio 2012中加載上述文件(是的,我告訴Visual Studio使用這個XSD文件來驗證命名空間)時,我得到這個:命名空間'http://'中的元素'root' peoplesoft.com/rootResponse'在'http://peoplesoft.com/rootResponse'命名空間中有無效的子元素'ReturnID'。預期的可能元素列表:'ReturnID'。

在這兩種情況下,它的嘎嘎叫有關ReturnID元素,但它說,它預計ReturnID元素?

回答

3

除非您處理的是非常不尋常的實例文檔,否則您的xs:schema元素應該帶有屬性elementFormDefault="qualified"。如果沒有這個,一個本地元素聲明(比如那個ReturnID)引用了一個沒有名字空間的元素,而不是目標名字空間中的一個元素。

+0

就是這樣!謝謝! –

1

基本上,XML驗證希望

ReturnID

<ReturnID xmlns="http://peoplesoft.com/rootResponse" /> 

ReturnID的xmlns = 「http://peoplesoft.com/rootResponse」

<ReturnID /> 

更改文檔如下:

<ps:root xmlns:ps="http://peoplesoft.com/rootResponse"> 
    <ReturnID /> 
</ps:root> 

編輯 的原因是,在您的文檔指定root xmlns="http://peoplesoft.com/rootResponse",所有內部要素再假設該命名空間爲好。

+0

謝謝,但有一個問題:如果我將'xmlns'更改爲'xmlns:ps',並且不會將'ReturnID'更改爲'ps:ReturnID',驗證錯誤仍會消失。這是爲什麼?我想如果你定義了一個新的namesapce,你必須明確地指定它的所有用途? –

+0

@ArenCambre - 抱歉我的錯誤 - 我會編輯 - 我沒有正確閱讀錯誤信息:( – StuartLC