Soap 松散xsd验证

Soap 松散xsd验证,soap,xsd,xsd-validation,Soap,Xsd,Xsd Validation,我正在调用一个Web服务,我想用xsd验证来验证soap响应。我真的不想做严格的xsd验证,我只想知道响应中是否存在某个元素“历史” 因此,当存在“历史元素”时,xsd验证应该是成功的,而当该元素不存在时,xsd验证应该是失败的。我从下面的xsd开始。我只想用我必须的“History”元素来扩展它。我怎样才能做到这一点 XSD: Soap响应 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelop

我正在调用一个Web服务,我想用xsd验证来验证soap响应。我真的不想做严格的xsd验证,我只想知道响应中是否存在某个元素“历史”

因此,当存在“历史元素”时,xsd验证应该是成功的,而当该元素不存在时,xsd验证应该是失败的。我从下面的xsd开始。我只想用我必须的“History”元素来扩展它。我怎样才能做到这一点

XSD:


Soap响应

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <p793:TuDetailsResponse xmlns:p793="http://gls-group.eu/Tracking/">
         <p793:History>
            <p793:Date>
               <p793:Year>2012</p793:Year>
               <p793:Month>10</p793:Month>
               <p793:Day>12</p793:Day>
               <p793:Hour>9</p793:Hour>
               <p793:Minut>52</p793:Minut>
            <p793:ReasonName/>
         </p793:History>
         <p793:History>
            <p793:Date>
               <p793:Year>2012</p793:Year>
               <p793:Month>10</p793:Month>
               <p793:Day>12</p793:Day>
               <p793:Hour>5</p793:Hour>
               <p793:Minut>45</p793:Minut>
            </p793:Date>
            <p793:ReasonName/>
         </p793:History>
      </p793:TuDetailsResponse>
   </soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Envelope xmlns:tns="http://gls-group.eu/Tracking/"
          xmlns="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ Main.xsd">
    <Body>
        <tns:TuDetailsResponse>
            <tns:History></tns:History>
        </tns:TuDetailsResponse>
    </Body>
</Envelope>

2012
10
12
9
52
2012
10
12
5.
45

在Hashmap/Map中添加名称空间为的所有必需实体,同时逐个解析响应读取实体,并检查实体是否存在于Map中。

在Hashmap/Map中添加名称空间为的所有必需实体,解析响应时,逐个读取实体并检查实体是否存在于映射中。

如果您让XSD验证器知道TuDetailsResponse和History元素,那么当它在any中看到它们时,它应该使用为它们定义的规则

Main.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

TuDetailsResponse.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Main.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

TuDetailsResponse.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

示例有效XML文件

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <p793:TuDetailsResponse xmlns:p793="http://gls-group.eu/Tracking/">
         <p793:History>
            <p793:Date>
               <p793:Year>2012</p793:Year>
               <p793:Month>10</p793:Month>
               <p793:Day>12</p793:Day>
               <p793:Hour>9</p793:Hour>
               <p793:Minut>52</p793:Minut>
            <p793:ReasonName/>
         </p793:History>
         <p793:History>
            <p793:Date>
               <p793:Year>2012</p793:Year>
               <p793:Month>10</p793:Month>
               <p793:Day>12</p793:Day>
               <p793:Hour>5</p793:Hour>
               <p793:Minut>45</p793:Minut>
            </p793:Date>
            <p793:ReasonName/>
         </p793:History>
      </p793:TuDetailsResponse>
   </soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Envelope xmlns:tns="http://gls-group.eu/Tracking/"
          xmlns="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ Main.xsd">
    <Body>
        <tns:TuDetailsResponse>
            <tns:History></tns:History>
        </tns:TuDetailsResponse>
    </Body>
</Envelope>

注意:主体元素可能导致生成警告

无效的XML

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Envelope xmlns:tns="http://gls-group.eu/Tracking/"
          xmlns="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ file:///C:/Temp/WSDL%20Sample.xsd">
    <Body>
        <tns:TuDetailsResponse>
        </tns:TuDetailsResponse>
    </Body>
</Envelope>

生成错误

(9:11)错误命名空间“”中的元素“TuDetailsResponse”的内容不完整。可能元素的列表应为命名空间“”中的“历史记录”

但是您确实应该使用一些soap包装器,这应该根据WSDL中的模式验证响应,并正确地整理所有失败消息。有一些工具可以为大多数平台和语言生成SOAP包装器


注意:本例中的验证是使用.Net类完成的。

如果让XSD验证程序知道TuDetailsResponse和History元素,那么当它在any中看到它们时,它应该使用为它们定义的规则

Main.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

TuDetailsResponse.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Main.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

TuDetailsResponse.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:ns0="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://schemas.xmlsoap.org/soap/envelope/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="C:\Temp\TuDetailsResponse.xsd"
               namespace="http://gls-group.eu/Tracking/" />
    <xs:element name="Envelope">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0"
                        maxOccurs="unbounded"
                        namespace="##any"
                        processContents="lax" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com)-->
<xs:schema xmlns:tns="http://gls-group.eu/Tracking/"
           elementFormDefault="qualified"
           targetNamespace="http://gls-group.eu/Tracking/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="TuDetailsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="History"
                            maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="0"
                                    maxOccurs="unbounded"
                                    namespace="##any" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

示例有效XML文件

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <p793:TuDetailsResponse xmlns:p793="http://gls-group.eu/Tracking/">
         <p793:History>
            <p793:Date>
               <p793:Year>2012</p793:Year>
               <p793:Month>10</p793:Month>
               <p793:Day>12</p793:Day>
               <p793:Hour>9</p793:Hour>
               <p793:Minut>52</p793:Minut>
            <p793:ReasonName/>
         </p793:History>
         <p793:History>
            <p793:Date>
               <p793:Year>2012</p793:Year>
               <p793:Month>10</p793:Month>
               <p793:Day>12</p793:Day>
               <p793:Hour>5</p793:Hour>
               <p793:Minut>45</p793:Minut>
            </p793:Date>
            <p793:ReasonName/>
         </p793:History>
      </p793:TuDetailsResponse>
   </soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Envelope xmlns:tns="http://gls-group.eu/Tracking/"
          xmlns="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ Main.xsd">
    <Body>
        <tns:TuDetailsResponse>
            <tns:History></tns:History>
        </tns:TuDetailsResponse>
    </Body>
</Envelope>

注意:主体元素可能导致生成警告

无效的XML

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Studio 2013 Designer Edition (Trial) 11.0.0.0 (http://www.liquid-technologies.com) -->
<Envelope xmlns:tns="http://gls-group.eu/Tracking/"
          xmlns="http://schemas.xmlsoap.org/soap/envelope/"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ file:///C:/Temp/WSDL%20Sample.xsd">
    <Body>
        <tns:TuDetailsResponse>
        </tns:TuDetailsResponse>
    </Body>
</Envelope>

生成错误

(9:11)错误命名空间“”中的元素“TuDetailsResponse”的内容不完整。可能元素的列表应为命名空间“”中的“历史记录”

但是您确实应该使用一些soap包装器,这应该根据WSDL中的模式验证响应,并正确地整理所有失败消息。有一些工具可以为大多数平台和语言生成SOAP包装器


注意:本例中的验证是使用.Net类完成的。

我使用外部工具用xsd验证soap响应。所以我只需要有一个xsd,我可以在我的工具中导入它。因此,我无法通过编程来解决这个问题。我正在使用外部工具用xsd验证soap响应。所以我只需要有一个xsd,我可以在我的工具中导入它。所以我无法通过编程来解决这个问题。