使用xsd文件验证肥皂xml响应

问题描述 投票:1回答:1

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

如果我在响应和模式中将soapenf完全排除在混合之外,它可以正常工作,但是我希望两者兼顾。

(fyi我想指出这个wsdl和xsd没有暴露在端点上,CISCO提供了wsdl和xsd文件的zip文件)然后你可以根据wsdl / xsd向服务器发送请求,它会起作用。但是cms.com上没有wsdl和xsd,或者安装了服务的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个回答
1
投票

事实证明,您只能验证其中一个。您无法加载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>

您无法同时验证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。您可以分离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>

您无法同时验证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文件进行验证花了几分钟,不得不放弃

最终的目标-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");
© www.soinside.com 2019 - 2024. All rights reserved.