php - 信封': No matching global declaration available for the validation root error when validating SOAP response in php

标签 php soap domdocument xsd-validation

我正在尝试验证 SOAP 响应信封,但不断收到错误

我有这个 SOAP 响应,我无法更改(response.xml)

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
    <GetVehicleMakesResponse xmlns="http://api.examle.com/">
        <GetVehicleMakesResult>
            <VehicleMakes xmlns="">
                <VehicleMake>
                    <MakeID>37</MakeID>
                    <MakeName>ALFA ROMEO</MakeName>
                </VehicleMake>
                <VehicleMake>
                    <MakeID>3</MakeID>
                    <MakeName>AUDI</MakeName>
                </VehicleMake>
                <VehicleMake>
                    <MakeID>19</MakeID>
                    <MakeName>BMW</MakeName>
                </VehicleMake>
            </VehicleMakes>
        </GetVehicleMakesResult>
    </GetVehicleMakesResponse>
</soap:Body>

我有一个 XSD 文件来验证这一点(GetVehicleMakes.xsd)

<xs:schema xmlns:tns="http://api.examle.com/" attributeFormDefault="unqualified" elementFormDefault="qualified" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
  <xs:element name="GetVehicleMakesResponse">
        <xs:complexType>
              <xs:sequence>
                    <xs:element name="GetVehicleMakesResult">
                          <xs:complexType>
                                <xs:sequence>
                                      <xs:element name="VehicleMakes">
                                            <xs:complexType>
                                                  <xs:sequence>
                                                        <xs:element maxOccurs="unbounded" name="VehicleMake">
                                                              <xs:complexType>
                                                                    <xs:sequence>
                                                                          <xs:element name="MakeID" type="xs:unsignedByte" />
                                                                          <xs:element name="MakeName" type="xs:string" />
                                                                    </xs:sequence>
                                                              </xs:complexType>
                                                        </xs:element>
                                                  </xs:sequence>
                                            </xs:complexType>
                                      </xs:element>
                                </xs:sequence>
                          </xs:complexType>
                    </xs:element>
              </xs:sequence>
        </xs:complexType>
  </xs:element>

当我使用 PHP DOM 文档验证 XSD 响应时

$xml = new DOMDocument(); 
$xml->load('response.xml');
$xml->schemaValidate('GetVehicleMakes.xsd')

我收到以下错误

Error 1845: Element '{http://www.w3.org/2003/05/soap-envelope}Envelope': No matching global declaration available for the validation root. in file:/P:/xampp/htdocs/test/response.xml on line 1

任何人都可以提供一些有关如何解决此错误的见解吗?

最佳答案

这是一个声明问题。在 xml 响应中,soap 信封元素的命名空间与 xsd 文件中定义的命名空间不同。

在xsd文件中定义如下:

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

但是响应中的 SOAP 信封元素是使用命名空间http://www.w3.org/2003/05/soap-envelope定义的。正如您的错误消息所示,针对 xsd 的验证不知道信封元素的特定命名空间。

由于您无法更改 SOAP 响应,因此您必须编辑 xsd 文件。只需导入正确的信封定义即可。

<xs:import namespace="http://www.w3.org/2003/05/soap-envelope" schemaLocation="http://www.w3.org/2003/05/soap-envelope"/>

您可能会意识到,验证会持续几秒钟。这是很正常的,因为 excessive traffic关于 w3c 发布的定义。 w3c 对定义请求设置了延迟。为了避免这些延迟,您可以将定义保存到本地文件并使用它们。

关于php - 信封': No matching global declaration available for the validation root error when validating SOAP response in php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56614821/

相关文章:

php - cakephp 中的模型和行为使用

PHP 将编号数组更改为带键的数组

php - 更快地将记录从 excel 文件 Laravel 5 插入数据库

c# - 将 C# 转换为 PHP

multithreading - 在Apache托管的Delphi创建的ISAPI dll中获取当前上下文ID

php - DOMDocument 删除脚本标签中的结束标签

php - 相同的 XPaths - 不同的结果

php - 发送电子邮件时未找到 laravel 类 'domdocument'

javascript - 如何使用jquery ajax进程访问单独的PHP页面中的函数?

php - 无法使用 mustUnderstand 设置 SOAP header