Cvc-elt.1:找不到元素宣言'肥皂:信封'

问题描述 投票:3回答:2

目前我正在使用XSD for SOAP XML,但是当我在FREEFORMATTER.COM上运行我的SOAP XML和XSD时,我收到此错误:

Cvc-elt.1:找不到元素宣言'肥皂:信封'..行'1',列'170'

这是我的SOAP XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Cancel_OrderLine xmlns="http://tempuri.org/">
      <Data>
        <Delivery>
          <Delivery_No>1605000194</Delivery_No>
          <Reason>qwertyu</Reason>
        </Delivery>
        <Delivery>
          <Delivery_No>1605000194</Delivery_No>
          <Reason>qwerty</Reason>
        </Delivery>
      </Data>
    </Cancel_OrderLine>
  </soap:Body>
</soap:Envelope>

这是我的XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <xs:element name="Cancel_OrderLineReq">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Data">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Delivery" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:int" name="Delivery_No"/>
                    <xs:element type="xs:string" name="Reason"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我该怎么做才能消除错误?

xml soap xsd
2个回答
2
投票

首先,必须将targetNamespace="http://tempuri.org/"添加到XSD的xs:schema元素中,以便将XSD应用于XML有效负载中使用的命名空间。

然后,您可以采用以下方法之一:更改XML或更改XSD。根据您控制的文件进行选择。

更改XML

xsi:schemaLocation="http://tempuri.org/ tempuri.xsd
                    http://schemas.xmlsoap.org/soap/envelope/ 
                    http://schemas.xmlsoap.org/soap/envelope/

soap:Envelope

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xsi:schemaLocation="http://tempuri.org/ tempuri.xsd
                                   http://schemas.xmlsoap.org/soap/envelope/ 
                                   http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Cancel_OrderLine xmlns="http://tempuri.org/">
      <Data>
        <Delivery>
          <Delivery_No>1605000194</Delivery_No>
          <Reason>qwertyu</Reason>
        </Delivery>
        <Delivery>
          <Delivery_No>1605000194</Delivery_No>
          <Reason>qwerty</Reason>
        </Delivery>
      </Data>
    </Cancel_OrderLine>
  </soap:Body>
</soap:Envelope>

更改XSD

<xs:import namespace="http://schemas.xmlsoap.org/soap/envelope/"   
           schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>

到你的XSD的xs:schema元素:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
           targetNamespace="http://tempuri.org/">

  <xs:import namespace="http://schemas.xmlsoap.org/soap/envelope/"   
             schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>

  <xs:element name="Cancel_OrderLineReq">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Data">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Delivery" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:int" name="Delivery_No"/>
                    <xs:element type="xs:string" name="Reason"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

这两种方法都可以成功验证SOAP信封及其有效负载。


0
投票

也许如果您针对xsd验证xml,则需要将builderFactory初始化为“知道名称空间” - 默认情况下它为false。

final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
final DocumentBuilder builder = builderFactory.newDocumentBuilder();
builder.parse(...);
© www.soinside.com 2019 - 2024. All rights reserved.