为SOAP响应创建XSD

时间:2015-09-15 15:52:33

标签: xml validation soap xsd

我想创建一个用于验证SOAP响应(XML)的XSD。但是,我正面临着错误 - "无法找到元素宣言' soap:Envelope'"。我尝试将soap:Envelope元素添加到XSD中,我收到此错误 - "' soap:Envelope'对于' NCName'"。

不是有效值

我的XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <GetValidOrdersBySalesOppAndLocationResponse xmlns="http://www.webserviceX.NET">
         <GetValidOrdersBySalesOppAndLocationResult>
            <Order>
               <ASV__c>0</ASV__c>
               <Date_Submitted__c>2015-08-25T11:02:07</Date_Submitted__c>
               <Order_Id>1365577</Order_Id>
               <End_Date__c>0001-01-01T00:00:00</End_Date__c>
               <Entitlement_Length__c>0</Entitlement_Length__c>
            </Order>
            <Order>
               <ASV__c>0</ASV__c>
               <Date_Submitted__c>2015-08-24T23:11:19.75</Date_Submitted__c>
               <Order_Id>1365580</Order_Id>
               <End_Date__c>0001-01-01T00:00:00</End_Date__c>
               <Entitlement_Length__c>0</Entitlement_Length__c>
            </Order>
            <Order>
               <ASV__c>0</ASV__c>
               <Date_Submitted__c>2015-08-25T11:19:10</Date_Submitted__c>
               <Order_Id>1365581</Order_Id>
               <End_Date__c>0001-01-01T00:00:00</End_Date__c>
               <Entitlement_Length__c>0</Entitlement_Length__c>
            </Order>
         </GetValidOrdersBySalesOppAndLocationResult>
      </GetValidOrdersBySalesOppAndLocationResponse>
   </soap:Body>
</soap:Envelope>

我的XSD如下:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="GetValidOrdersBySalesOppAndLocationResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="GetValidOrdersBySalesOppAndLocationResult">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Order" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:byte" name="ASV__c"/>
                    <xs:element type="xs:dateTime" name="Date_Submitted__c"/>
                    <xs:element type="xs:int" name="Order_Id"/>
                    <xs:element type="xs:dateTime" name="End_Date__c"/>
                    <xs:element type="xs:byte" name="Entitlement_Length__c"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我使用了在线XSD生成器 - http://www.freeformatter.com/xsd-generator.html和验证者 - http://www.freeformatter.com/xml-validator-xsd.html

我还检查了其他stackoverflow问题。有一个类似于我的确切问题,但它没有一个可以在这里找到的工作解决方案 - XSD for soap result not working。由于我的低分,我无法评论要求OP的更新。请帮忙。

更新: 添加导入工作!

工作代码(添加第二行):

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET"     xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/> //Added import
  <xs:element name="GetValidOrdersBySalesOppAndLocationResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="GetValidOrdersBySalesOppAndLocationResult">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Order" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:byte" name="ASV__c"/>
                    <xs:element type="xs:dateTime" name="Date_Submitted__c"/>
                    <xs:element type="xs:int" name="Order_Id"/>
                    <xs:element type="xs:dateTime" name="End_Date__c"/>
                    <xs:element type="xs:byte" name="Entitlement_Length__c"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:2)

您应该考虑使用原始XSD for SOAP信封并使用它。然后使用xs:import向您自己的XSD扩展您要验证的必要数据类型。使用SOAP XSD将允许您验证任何SOAP请求。要验证您自己的类型,只需为通用部分添加自己的类型。

更好的方法通常是仅验证自定义部分,因为如果信封不是有效的SOAP,任何符合SOAP的服务服务器或客户端都将抛出错误。如果您只关注那个部分,它还可以节省您的时间。