0

我需要更改什么,以便 FieldRef 下的 Name 节点是 FieldRef 的属性,而不是子节点?

Suds 目前生成以下肥皂:

<ns0:query>
  <ns0:Where>
    <ns0:Eq>
      <ns0:FieldRef>
        <ns0:Name>_ows_ID</ns0:Name>
      </ns0:FieldRef>
      <ns0:Value>66</ns0:Value>
    </ns0:Eq>
  </ns0:Where>
</ns0:query>

我需要的是这个:

<ns0:query>
  <ns0:Where>
     <ns0:Eq>
        <ns0:FieldRef Name="_ows_ID">
        </ns0:FieldRef>
        <ns0:Value>66</ns0:Value>
     </ns0:Eq>
  </ns0:Where>
</ns0:query>

第一个 xml 结构是由 suds 从下面的代码生成的。

q = c.factory.create('GetListItems.query')
q['Where']=InstFactory.object('Where')
q['Where']['Eq']=InstFactory.object('Eq')
q['Where']['Eq']['FieldRef']=InstFactory.object('FieldRef')
q['Where']['Eq']['FieldRef'].Name='_ows_ID'
q['Where']['Eq']['Value']='66'

print(q)导致

(query){
   Where = 
      (Where){
         Eq = 
            (Eq){
               FieldRef = 
                  (FieldRef){
                     Name = "_ows_ID"
                  }
               Value = "66"
            }
      }
 }

这是进行创建soap请求的WS调用的代码

c = client.Client(url='https://community.site.edu/_vti_bin/Lists.asmx?WSDL',
                  transport=WindowsHttpAuthenticated(username='domain\user',
                                                     password='password')
                                             )
ll= c.service.GetListItems(listName="{BD59F6D9-AB4B-474D-BCC7-E4B4BEA7EB27}",
                             viewName="{407A6AB9-97CF-4E1F-8544-7DD67CEA997B}",
                             query=q
                             )
4

1 回答 1

0
from suds.sax.element import Element
#create the nodes
q = Element('query')
where=Element('Where')
eq=Element('Eq')
fieldref=Element('FieldRef')
fieldref.set('Name', '_ows_ID')
value=Element('Value')
value.setText('66')

#append them
eq.append(fieldref)
eq.append(value)
where.append(eq)
q.append(where)

https://fedorahosted.org/suds/wiki/TipsAndTricks

包括文字 XML

要将文字(未转义)XML 作为对象属性的参数值,您需要将对象属性的参数值设置为 sax 元素。编组器旨在简单地附加和附加已经是 XML 的内容。

例如,您希望将以下 XML 作为参数传递:

<query> <name>Elmer Fudd</name>
<age unit="years">33</age>
<job>Wabbit Hunter</job> </query>

可以按如下方式完成:

from suds.sax.element import Element
query = Element('query')
name = Element('name').setText('Elmer Fudd')
age = Element('age').setText('33')
age.set('units', 'years')
job = Element('job').setText('Wabbit Hunter')
query.append(name)
query.append(age)
query.append(job)
client.service.runQuery(query)
于 2009-06-12T15:59:48.900 回答