使用xsd文件验证Soap xml响应

使用xsd文件验证Soap xml响应,xml,cocoa,soap,xsd,cisco-axl,Xml,Cocoa,Soap,Xsd,Cisco Axl,使用 如果我将soapenf从这里的响应和模式中完全去掉,它可以很好地工作,但我希望两者都可以 (仅供参考,我想指出此wsdl和xsd未在端点上公开,CISCO提供wsdl的zip文件和xsd文件)然后您可以根据wsdl/xsd向服务器发送请求,它将正常工作。但是wsdl和xsd在cisco.com或安装了服务的vm或域上不可用) 如果我指向文件,这在c#中非常有效,但是我想将xml文档加载到NSXMLDocument变量文档中,让它指向自己的schemaFile,然后调用validate 我有

使用

如果我将soapenf从这里的响应和模式中完全去掉,它可以很好地工作,但我希望两者都可以

(仅供参考,我想指出此wsdl和xsd未在端点上公开,CISCO提供wsdl的zip文件和xsd文件)然后您可以根据wsdl/xsd向服务器发送请求,它将正常工作。但是wsdl和xsd在cisco.com或安装了服务的vm或域上不可用)

如果我指向文件,这在c#中非常有效,但是我想将xml文档加载到NSXMLDocument变量文档中,让它指向自己的schemaFile,然后调用validate

我有以下soapxml

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

10.5.2.11900(3)
我从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>

所有响应都必须扩展abstractResponse。
请注意,我必须将下面的内容添加到xsd文件中,以便它通过SOAP错误

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

它在在线工具中工作

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

我已经尝试过使用在线工具的各种方法,但它永远不会被验证。它目前托管在(我希望不是因为它是一个免费的主机,我有问题),但也应该有方法来完成这个使用文件:///

我使用的是macOS和objective-c,但所有的代码都在操作soap xml响应头,并将file:///. 我也试过http://了

谁能解开这个绝对的谜团?从网上的一些例子来看,这似乎是如此简单明了

提前谢谢你的帮助


这家伙从来没有得到答案,结果你只能验证其中一个。不能在一次调用中加载soap响应并验证soap及其内部的xml。您可以分离soap和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>
<?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>
    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");