我正在尝试验证 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/