代码之家  ›  专栏  ›  技术社区  ›  Mr. Llama

PYXB生成的模块使用匿名结构名?

  •  0
  • Mr. Llama  · 技术社区  · 7 年前

    我用 this XML schema definition :

    $ pyxbgen -u "https://cwmp-data-models.broadband-forum.org/cwmp-1-2.xsd" -m cwmp
    [Warnings about duplicate SOAP encoding/envelope complex types omitted]
    Python for urn:dslforum-org:cwmp-1-2 requires 3 modules
    

    …我有以下有效资料 XML 与架构匹配的文档:

    <SOAP-ENV:Envelope
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:cwmp="urn:dslforum-org:cwmp-1-2"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    >
        <SOAP-ENV:Header>
            <cwmp:ID SOAP-ENV:mustUnderstand="1">Testing123</cwmp:ID>
            <cwmp:HoldRequests SOAP-ENV:mustUnderstand="1">1</cwmp:HoldRequests>
        </SOAP-ENV:Header>
        <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <cwmp:GetParameterNames>
                <ParameterPath>InternetGatewayDevice.</ParameterPath>
                <NextLevel>1</NextLevel>
            </cwmp:GetParameterNames>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    当我使用模块时,我可以看到 Header Body 元素很好,但是所有其他元素都以自动生成的类型名结束,并且只能通过 wildcardElements :

    >>> import cwmp
    >>>
    >>> with open("sample.xml", "r") as f:
    ...   doc = f.read()
    
    >>> xml = cwmp.CreateFromDocument(doc)
    
    >>> xml.Header
    <_soapenv.Header_ object at 0x6fffde73ad0>
    
    >>> xml.Body
    <_soapenv.Body_ object at 0x6fffdcb3390>
    
    # I expect these to exist, but apparently they don't
    >>> xml.Header.ID
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Header_' object has no attribute 'ID'
    
    >>> xml.Header.HoldRequests
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Header_' object has no attribute 'HoldRequests'
    
    >>> xml.Body.GetParameterNames
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Body_' object has no attribute 'GetParameterNames'
    
    # I can see that they have child elements of the correct type
    #   so why can't I access them by name?
    >>> xml.Body.wildcardElements()
    [<cwmp.CTD_ANON_8 object at 0x6fffdcb36d0>]
    
    >>> cwmp.GetParameterNames()
    <cwmp.CTD_ANON_8 object at 0x6fffdedba90>
    
    >>> xml.Header.wildcardElements()
    [<cwmp.CTD_ANON_56 object at 0x6fffdcb3310>, <cwmp.CTD_ANON_57 object at 0x6fffdcb34d0>]
    
    >>> cwmp.ID("Foo")
    <cwmp.CTD_ANON_56 object at 0x6fffde73250>
    
    >>> cwmp.HoldRequests(True)
    <cwmp.CTD_ANON_57 object at 0x6fffe025d90>
    

    怎么回事?
    XSD坏了吗?我是不是误解了如何使用 PYXB ?

    我的最终目标是能够通过tag/name/type访问这些元素。有什么办法可以做到这一点吗?

    0 回复  |  直到 7 年前