tags:

views:

176

answers:

1

What do I need to change so the Name node under FieldRef is an attribute of FieldRef, and not a child node?

Suds currently generates the following soap:

<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>

What I need is this:

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

The first xml structure is generated by suds from the below code.

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'

and print(q) results in

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

Here's the code that makes the WS call that creates the soap request

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
                             )
A: 
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

Including Literal XML

To include literal (not escaped) XML as a parameter value of object attribute, you need to set the value of the parameter of the object attribute to be a sax Element. The marshaller is designed to simply attach and append content that is already XML.

For example, you want to pass the following XML as a parameter:

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

The can be done as follows:

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)
antony.trupe