xml - 使用 xsd 文件验证 Soap xml 响应
问题描述
使用https://www.freeformatter.com/xml-validator-xsd.html
如果我将 soapenf 完全从响应和模式中的混合中取出,它可以正常工作,但我想两者都做。
(仅供参考,我想指出这个 wsdl 和 xsd 没有暴露在端点上,CISCO 提供了 wsdl 的 zip 文件和 xsd 文件)然后您可以通过 wsdl/xsd 向服务器发送请求,它会起作用。但是 wsdl 和 xsd 在 cisco.com 或您安装了该服务的 vm 或域上不可用)
如果我指向文件,这在 c# 中非常有效,但我想将 xml 文档加载到 NSXMLDocument 变量文档中并让它指向它自己的 schemaFile 并调用 validate
我有以下肥皂 xml
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Body>
<ns:getCCMVersionResponse xmlns:ns="http://www.cisco.com/AXL/API/10.5"><return><componentVersion>
<version>10.5.2.11900(3)</version>
</componentVersion>
</return>
</ns:getCCMVersionResponse>
</soapenv:Body>
</soapenv:Envelope>
我从 cisco 提供的 xsd 文件中提取了最少的内容,并且在线工具可以使用
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:axlapi="http://www.cisco.com/AXL/API/10.5"
attributeFormDefault="unqualified" elementFormDefault="unqualified"
targetNamespace="http://www.cisco.com/AXL/API/10.5" version="10.5">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
<xsd:complexType name="GetCCMVersionRes">
<xsd:complexContent>
<xsd:extension base="axlapi:APIResponse">
<xsd:sequence>
<xsd:element name="return">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="componentVersion">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="version" type="axlapi:String50"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="getCCMVersionResponse" type="axlapi:GetCCMVersionRes"/>
<xsd:simpleType name="String50">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType abstract="true" name="APIResponse">
<xsd:annotation>
<xsd:documentation>All responses must extend abstractResponse.</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="sequence" type="xsd:unsignedLong" use="optional"/>
</xsd:complexType>
</xsd:schema>
请注意,我必须在 xsd 文件中添加以下内容才能通过 SOAP 错误
<xsd:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
它适用于在线工具
现在这对于一个在线工具来说非常棒,但我想验证文档本身。这是通过添加到 Soap 请求信封元素“noNamespaceSchemaLocation”或“schemaLocation”以及 xsd 文件(或我在上面创建的部分文件)的 http 路径来完成的(如果您阅读了在线工具)
我已经尝试了各种使用在线工具的方法,但它永远不会验证。它目前托管在http://test123a.epizy.com/getCCMVersion.xsd(我希望它不是因为它是一个免费的主机,我有问题),但也应该有办法使用 file:///
我正在使用 macOS 和objective-c,但所有代码都在做,正在操作soap xml 响应标头,并放置file:/// 的位置。我也试过http://。
谁能解开这个绝对的谜团?从网上的一些例子来看,这似乎很简单......
在 Cocoa 中使用 xsd 文件验证 XML Schema?
http://answerqueen.com/2j/q7vz0zv32j
如何修复soapenv:XSD模式中的信封问题,同时使用SOAP请求/响应进行验证
https://code.i-harness.com/en/q/8bfd7
Cvc-elt.1:找不到元素“soap:Envelope”的声明
提前谢谢你的帮助
这家伙从来没有得到答案SOAP 响应模式验证
解决方案
原来你只能验证一个或另一个。您无法在一次调用中加载soap 响应并验证soap 以及其中的xml。您可以将肥皂和 xml 分开并分别验证。
这是这样做的正确代码
<?xml version='1.0' encoding='UTF-8'?>"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getCCMVersionResponse xmlns:ns="http://www.cisco.com/AXL/API/10.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cisco.com/AXL/API/10.5
/Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd">
<return>
<componentVersion>
<version>10.5.2.11900(3)</version>
</componentVersion>
</return>
</ns:getCCMVersionResponse>
</soapenv:Body>
您不能同时验证内部的 SOAP 和 XML。您必须使用 xpath 才能访问 xml 并可以进行验证,因为验证 soap 并不那么重要。
有趣的是在 xml
我无法使用 xsi:noNameSpaceSchemaLocation="file:///Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd">
如在 xml xsi:schemaLocation="http://www.cisco.com/AXL/API/10.5
/Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd"> 已被引用为模式命名空间
所以必须在上面使用替代方案。
此外,我必须编辑模式文件以修复一些命名空间问题,根据 xml 响应 xmlns:ns="http://www.cisco.com/AXL/API/10.5",ns 是命名空间,
但是在模式文件中,它是作为 axlapi 的引用,所有引用都更改为 ns
希望这将有助于未来的人。仅供参考,指向整个 AXLSoap.xsd 文件进行验证需要几分钟,并且不得不放弃
原来你只能验证一个或另一个。您无法在一次调用中加载soap 响应并验证soap 以及其中的xml。您可以将肥皂和 xml 分开并分别验证。
这是这样做的正确代码
<?xml version='1.0' encoding='UTF-8'?>"
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getCCMVersionResponse xmlns:ns="http://www.cisco.com/AXL/API/10.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cisco.com/AXL/API/10.5
/Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd">
<return>
<componentVersion>
<version>10.5.2.11900(3)</version>
</componentVersion>
</return>
</ns:getCCMVersionResponse>
</soapenv:Body>
您不能同时验证内部的 SOAP 和 XML。您必须使用 xpath 才能访问 xml 并可以进行验证,因为验证 soap 并不那么重要。
有趣的是在 xml
我无法使用 xsi:noNameSpaceSchemaLocation="file:///Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd">
如在 xml xsi:schemaLocation="http://www.cisco.com/AXL/API/10.5
/Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd"> 已被引用为模式命名空间
所以必须在上面使用替代方案。
此外,我必须编辑模式文件以修复一些命名空间问题,根据 xml 响应 xmlns:ns="http://www.cisco.com/AXL/API/10.5",ns 是命名空间,
但是在模式文件中,它是作为 axlapi 的引用,所有引用都更改为 ns
希望这将有助于未来的人。仅供参考,指向整个 AXLSoap.xsd 文件进行验证需要几分钟,并且不得不放弃
最终的objective-c NSXMLDocument 函数,用于处理验证soap 或xml 在这里。
BOOL validateDocumentUsingXMLSchemaAtPath( NSXMLDocument *doc,
NSString* xsdPath, NSString *schemaNameSpace )
{
BOOL validationResult = NO; // Default to failing validation.
// Get the root of the document.
NSXMLElement *rootElement = [doc rootElement];
// Add the XMLSchema-instance namespace, if it doesn't already exist
NSXMLNode *namespace = [NSXMLNode namespaceWithName:@"xsi"
stringValue:@"http://www.w3.org/2001/XMLSchema-instance"];
[rootElement addNamespace:namespace];
// Add the No Namespace Schema Location attribute referencing the local XSD file, and associate the XML Schema for validation.
NSURL *xsdURL = [NSURL fileURLWithPath:xsdPath];
NSString *schemaLocation = nil;
if ( namespace == nil )
{
schemaLocation = @"noNamespaceSchemaLocation";
schemaNameSpace = @"";
}
else
{
schemaLocation = @"schemaLocation";
}
NSString *schemaSpace = [NSString stringWithFormat:@"%@%@%@", schemaNameSpace, ([schemaNameSpace length] > 0) ? @" " : @"", [xsdURL description]];
NSXMLNode *schemaAttribute = [NSXMLNode attributeWithName:[NSString stringWithFormat:@"xsi:%@", schemaLocation]
stringValue:schemaSpace];
[rootElement addAttribute:schemaAttribute];
// Validate the document
NSError *error = nil;
validationResult = [doc validateAndReturnError:&error];
if ( !validationResult )
NSLog( @" %@", [error localizedDescription] );
return validationResult;
}
//usage
//find your soap, or xml document and put it in xmlDoc
BOOL ret = validateDocumentUsingXMLSchemaAtPath(xmlDoc,xsdURL, @"http://www.cisco.com/AXL/API/10.5");
推荐阅读
- django - Django REST Serializer 使用错误的模型进行序列化
- python - python文件写入程序运行时如何更新桌面上的文件大小
- javascript - 使用 d3.js 更新表数据
- c# - C#捕获从不在进程中的函数返回的异常?
- r - 如何设置仅在输入 3 时才显示集合向量的函数?
- javascript - 如果 URI 没有改变,例如在单页应用程序上,如何检测用户是否在新页面上?
- angular - Angular Kendo UI 全局访问
- php - 内连接循环通过
- git - 如何 git rebase 从另一个分支直接到 master 分支?
- javascript - 受控数字比例映射