【问题标题】:Cvc-elt.1: Cannot Find The Declaration Of Element 'soap:Envelope'Cvc-elt.1:找不到元素“soap:Envelope”的声明
【发布时间】:2017-02-05 21:04:52
【问题描述】:

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

Cvc-elt.1:找不到元素“soap:Envelope”的声明。“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


    【解决方案1】:

    首先,您必须将 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 信封及其有效负载。

    【讨论】:

    • 你好,谢谢它在freeformatter.com/xml-validator-xsd.html 上工作,但是当我在我的 ColdFusion 测试页面中实现时,它给了我相同的结果
    • 你没有说你尝试了两种方法中的哪一种。请记住,仅更改 XSD 仍然需要通过某种方式将 XML 与 XSD 关联起来;而在 XML 中添加 xsi:schemaLocation 为解析器提供了关于在哪里可以找到 XSD 的提示。
    • 您好,两种方法我都试过了。两者都可以在 freeformatter.com/xml-validator-xsd.html 网站上使用。但不在我的coldfusion测试页面中。首先,我将 xml 分配给我的测试页 b4 中的一个变量,基于它到 xsd。
    • 从 XML 和 XSD 的角度来看,这两种方法都是正确的。无论您遇到什么麻烦,都只能与您在 Coldfusion 中的验证方式有关。我建议您为此提出一个新问题,确保显示您的代码并指定 CF 的版本和所涉及的任何库。
    【解决方案2】:

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

    final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    final DocumentBuilder builder = builderFactory.newDocumentBuilder();
    builder.parse(...);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 2015-11-25
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多