【问题标题】:Soap xml response validate with xsd file使用 xsd 文件验证 Soap xml 响应
【发布时间】:2018-12-15 10:24:51
【问题描述】:

使用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://。

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

Validate XML Schema with xsd file in Cocoa?

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

How to fix soapenv:Envelope issue in XSD schema while validating with SOAP request/response

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

How to reference a local XML Schema file correctly?

Cvc-elt.1: Cannot Find The Declaration Of Element 'soap:Envelope'

提前感谢您的帮助

这家伙从来没有得到答案SOAP Response Schema Validation

【问题讨论】:

    标签: xml cocoa soap xsd cisco-axl


    【解决方案1】:

    原来你只能验证一个或另一个。您无法在一次调用中加载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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 2010-09-26
      • 2012-06-04
      • 2012-07-23
      • 1970-01-01
      相关资源
      最近更新 更多