使用 xsd 文件验证 Soap xml 响应 [英] Soap xml response validate with xsd file

查看:42
本文介绍了使用 xsd 文件验证 Soap xml 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 https://www.freeformatter.com/xml-validator-xsd.html

如果我将 soapenf 完全排除在响应和架构中,它可以正常工作,但我想两者都做.

If i take soapenf completely out of the mix here in the response and the schema, it works fine, but would like to do both.

(我想指出这个 wsdl 和 xsd 没有暴露在端点上,CISCO 提供了 wsdl 的 zip 文件和 xsd 文件)然后您可以通过 wsdl/xsd 向服务器发送请求,它将工作.但是 wsdl 和 xsd 在 cisco.com 或您安装了服务的 vm 或域上不可用)

(fyi i would like to point out this wsdl and xsd is not exposed on the endpoint, CISCO provides the zip file of the wsdl an xsd file) You then can send requests to the server per the wsdl/xsd and it will work. But the wsdl and xsd are not available on the cisco.com or the vm or domain you have the service installed)

如果我指向文件,这在 c# 中完美运行,但我想将 xml 文档加载到 NSXMLDocument 变量文档中并让它指向它自己的 schemaFile 并调用 validate

This works perfectly in c# if i point to files, but i would like to load the xml document into NSXMLDocument variable document and have it point to its own schemaFile and just call 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 文件中提取了最少的内容,并且在线工具可以使用

I took the bare minimum out of the xsd file that was provided by cisco and the online tool works with

<?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 错误

Please note i had to add below to the xsd file for it to get past a SOAP error

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

它可以在在线工具中使用

And it works in the online tool

现在这对于一个在线工具来说非常棒,但我想验证文档本身.这是通过添加到 Soap 请求信封元素noNamespaceSchemaLocation"或schemaLocation"以及 xsd 文件(或我在上面创建的部分文件)的 http 路径来完成的(如果您阅读了在线工具)

Now this is great and all for an online tool, but i would like to verify the document itself. This is done (if you read the online tool), by adding to the Soap request envelope element "noNamespaceSchemaLocation" or "schemaLocation" and a http path to the xsd file (or the partial file i created above)

我已经尝试了各种使用在线工具的方法,但它永远无法验证.它目前托管在 http://test123a.epizy.com/getCCMVersion.xsd (我希望这不是因为它是一个免费的主机,我有问题),但也应该有办法使用 file:///

I have tried all sorts of ways using the online tool, and it would never validate. its currently hosted at http://test123a.epizy.com/getCCMVersion.xsd (i hope its not because its a free host that im having a problem), but there should also be ways to accomplish this using file:///

我正在使用 macOS 和 Objective-c,但所有代码都在做,正在操作soap xml 响应标头,并放置 file:///的位置.我也试过http://.

I am using macOS, and objective-c, but all the code is doing, is manipulating the soap xml response header, and putting the location of file:///. I have also tried http:// as well.

谁能解开这个绝对的谜团?从网上的一些例子来看,似乎很简单......

Can anyone solve this absolute mystery? It seems so plain and simple from some of the examples online....

使用 Cocoa 中的 xsd 文件验证 XML Schema?

http://answerqueen.com/2j/q7vz0zv32j

如何修复soapenv:使用 SOAP 请求/响应验证时 XSD 架构中的信封问题

https://code.i-harness.com/en/q/8bfd7

如何正确引用本地 XML Schema 文件?

Cvc-elt.1:找不到元素声明'soap:Envelope'

提前感谢您的帮助

这家伙从来没有得到答案SOAP 响应架构验证

this guy never got an answer SOAP Response Schema Validation

推荐答案

原来你只能验证一个或另一个.您无法在一次调用中加载soap 响应并验证soap 以及其中的xml.您可以将肥皂和 xml 分开并分别验证.

Turns out you can only validate one or the other. You cannot load the soap response and validate the soap, and the xml inside it all in one call. You can separate the soap, and the xml and validate the separately.

这是正确的代码

<?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 并不那么重要.

You cannot validate the SOAP and the XML inside at the same time. You will have to use xpath to get to xml and can validate, as validating soap is not as important.

有趣的是在 xml 中

What was interesting is in the xml

我无法使用 xsi:noNameSpaceSchemaLocation="file:///Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd">

I could not use 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"> 已被引用为模式命名空间

as in the xml xsi:schemaLocation="http://www.cisco.com/AXL/API/10.5
/Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd"> is already referenced as a schema namespace

因此必须在上面使用替代方案.

So the alternative had to be used above.

此外,我必须编辑架构文件以修复一些命名空间问题,根据 xml 响应 xmlns:ns="http://www.cisco.com/AXL/API/10.5",ns 是命名空间,

Also, I had to edit the schema file to fix some namespace issues, per the xml response xmlns:ns="http://www.cisco.com/AXL/API/10.5", ns is the namespace,

但在架构文件中,它是作为 axlapi 的引用,所有引用都更改为 ns

But in the schema file, it is references as axlapi , all references where changed to ns

希望这对将来的某人有所帮助.仅供参考,指向整个 AXLSoap.xsd 文件进行验证需要几分钟,并且不得不放弃

Hopefully this will help somebody in the future. FYI, pointing the the entire AXLSoap.xsd file for validation took a few minutes, and had to be abandoned

原来你只能验证一个或另一个.您无法在一次调用中加载soap 响应并验证soap 以及其中的xml.您可以将肥皂和 xml 分开并分别验证.

Turns out you can only validate one or the other. You cannot load the soap response and validate the soap, and the xml inside it all in one call. You can separate the soap, and the xml and validate the separately.

这是正确的代码

<?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 并不那么重要.

You cannot validate the SOAP and the XML inside at the same time. You will have to use xpath to get to xml and can validate, as validating soap is not as important.

有趣的是在 xml 中

What was interesting is in the xml

我无法使用 xsi:noNameSpaceSchemaLocation="file:///Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd">

I could not use 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"> 已被引用为模式命名空间

as in the xml xsi:schemaLocation="http://www.cisco.com/AXL/API/10.5
/Users/michael/Downloads/axlsqltoolkit/schema/current/getCCMVersion.xsd"> is already referenced as a schema namespace

因此必须在上面使用替代方案.

So the alternative had to be used above.

此外,我必须编辑架构文件以修复一些命名空间问题,根据 xml 响应 xmlns:ns="http://www.cisco.com/AXL/API/10.5",ns 是命名空间,

Also, I had to edit the schema file to fix some namespace issues, per the xml response xmlns:ns="http://www.cisco.com/AXL/API/10.5", ns is the namespace,

但在架构文件中,它是作为 axlapi 的引用,所有引用都更改为 ns

But in the schema file, it is references as axlapi , all references where changed to ns

希望这对将来的某人有所帮助.仅供参考,指向整个 AXLSoap.xsd 文件进行验证需要几分钟,并且不得不放弃

Hopefully this will help somebody in the future. FYI, pointing the the entire AXLSoap.xsd file for validation took a few minutes, and had to be abandoned

最终的objective-c NSXMLDocument 函数,用于处理对soap 或xml 的验证.

The final objective-c NSXMLDocument function, to handle either validating the soap or the xml is here.

    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");

这篇关于使用 xsd 文件验证 Soap xml 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆