【问题标题】:python xsd within soap response肥皂响应中的python xsd
【发布时间】:2016-07-19 13:42:19
【问题描述】:

我正在使用 spyne 在服务器端使用 python 生成 Web 服务以与 excel 客户端(xmla 插件)进行通信,并且在生成与客户端请求匹配的肥皂响应时遇到了一些困难, 我想要的是这样的肥皂响应(在 root 标记下带有“xsd:”的 schema 引用 xmlns:xsd 中的="http://www.w3.org/2001/XMLSchema

<soap:Envelope xmlns="urn:schemas-microsoft-com:xml-analysis">
    <soap:Body>
       <DiscoverResponse xmlns="urn:...">
          <return> 
              <root xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                    xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"
                    ....>

                  < xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset" 


                xmlns:sql="urn:schemas-microsoft-com:xml-sql">
                <xsd:element name="root">
                <xsd:complexType>
                <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                <xsd:element name="row" type="row"/>
                </xsd:sequence>
                </xsd:complexType>
                </xsd:element>
                <xsd:simpleType name="uuid">
                <xsd:restriction base="xsd:string">
                <xsd:pattern value="[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}"/>
                </xsd:restriction>
                </xsd:simpleType>
                <xsd:complexType name="xmlDocument">
                <xsd:sequence>
                <xsd:any/>
                </xsd:sequence>
                </xsd:complexType>
                <xsd:complexType name="row">
                <xsd:sequence>
                <xsd:element sql:field="PropertyName" name="PropertyName" type="xsd:string"/>
                <xsd:element sql:field="PropertyDescription" name="PropertyDescription" type="xsd:string" minOccurs="0"/>
                <xsd:element sql:field="PropertyType" name="PropertyType" type="xsd:string" minOccurs="0"/>
                <xsd:element sql:field="PropertyAccessType" name="PropertyAccessType" type="xsd:string"/>
                <xsd:element sql:field="IsRequired" name="IsRequired" type="xsd:boolean" minOccurs="0"/>
                <xsd:element sql:field="Value" name="Value" type="xsd:string" minOccurs="0"/>
                </xsd:sequence>
                </xsd:complexType>
                </xsd:schema>

            </root>
        </return>
    </DiscoverResponse>
</soap:Body>

我每次用 spyne 得到的结果都不包含 xsd: :

  <soap11env:Body>
<tns:DiscoverResponse>
  <tns:return>
    <ns2:root xmlns:ns2="urn:schemas-microsoft-com:xml-analysis:rowset">
      <ns3:schema xmlns:ns3="services.models" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"/>
    </ns2:root>
  </tns:return>
</tns:DiscoverResponse>

这是我的 Python 代码:
在models.py文件中:

class schema(ComplexModel):
targetNamespace = XmlAttribute(Unicode)
__type_name__ = "schema"

class DiscoverResponse(ComplexModel):
__namespace__ = "urn:schemas-microsoft-com:xml-analysis:rowset"
root = Array(schema.customize(max_occurs='unbounded'))

在 xmla.py 文件中:

class xmla_provider(ServiceBase):

    @rpc(Unicode,
     Restrictionlist,
     Propertielist,
     _returns=[DiscoverResponse],
     _out_variable_names=["return"])

        def Discover(ctx, RequestType, Restrictions, Properties):
            response = DiscoverResponse()
            response.root = schema(targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset"),
            return response

解决我尝试添加的问题 __ 命名空间 __ = "http://www.w3.org/2001/XMLSchema" 在 类架构 中,我遇到了这个问题
如果 child_ns != ns 而不是 self.imports[ns] 和 \ 中的 child_ns KeyError: 'http://www.w3.org/2001/XMLSchema'

想到的另一个解决方案不是一个好的解决方案,是强制返回所有的肥皂响应

<xsd:schema targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset" 
            xmlns:sql="urn:schemas-microsoft-com:xml-sql">....  

root 属性为 Unicode
这是代码: 在 xmla.py 中

  response.root = "\n  < xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='urn:schemas-microsoft-com:xml-analysis:rowset' ....

在model.py中

class DiscoverResponse(ComplexModel):
__namespace__ = "urn:schemas-microsoft-com:xml-analysis:rowset"
root = Unicode

我得到了这个输出

DEBUG:spyne.protocol.xml:Response <soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:schemas-microsoft-com:xml-analysis">
 <soap11env:Body>
<tns:DiscoverResponse>
  <tns:return>
    <ns0:root xmlns:ns0="urn:schemas-microsoft-com:xml-analysis:rowset">

      &lt; xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'
      xmlns='urn:schemas-microsoft-com:xml-analysis:rowset'
      xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
      xmlns:sql='urn:schemas-microsoft-com:xml-sql' 

      ....

      /xsd:schema

  </ns0:root>
  </tns:return>
</tns:DiscoverResponse>

所有 字符都替换为“& lt;”和“>” (正如我所说,这不是一个好的解决方案,通常我必须解决名称空间的问题)

请提供任何解决问题的建议,或者在最坏的情况下,请提供任何关于 python 中允许这种响应的soap库的建议

【问题讨论】:

  • 我很困惑,最近每个人都在问 Spyne 问题时没有一点 Spyne 代码。我需要看代码!错误!来吧伙计们,帮我帮你!
  • 你设置_returns=AnyXml了吗?你是如何产生响应的?
  • 抱歉不清楚,我更新了我的问题@BurakArslan

标签: python soap spyne xmla


【解决方案1】:

你需要切换到裸模式并像这样返回AnyXml

class DiscoverRequest(ComplexModel):
    RequestType = Unicode
    Restrictions = Restrictionlist
    Properties = Propertielist


class HelloWorldService(ServiceBase):
    @rpc(DiscoverRequest, _returns=AnyXml, _body_style="bare")
    def Discover(ctx, request):
        return etree.fromstring("""<root>whatever</root>""")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2014-09-05
    • 2011-08-17
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    相关资源
    最近更新 更多