views:

354

answers:

1

hi all,

I can not seem to find out how to set an attribute to a SOAP request without using the XSD_ANYXML encoding.

The request parameter should look as follows

<request
    xmlns:ns="/some/ns">
    ...
        <ns:parameter attr="some attribute">
            value
        </ns:parameter>
    ...
</request>

Of course the following code works, but it's rather ugly (ugly, because it uses string concatenation where it should use the SOAP_Client API and because it does not use the general namespace)

$param = new SoapVar(
    '<ns_xxx:parameter xmlns:ns_xxx="/some/ns" attr="some attribute">
        value
     </ns_xxx:parameter>',
    XSD_ANYXML
);

Is there a better way to create a SOAP request parameter with a namespace and an attribute?

I am looking for s.th. like the following (this is just some pseudo code using the SoapVar API):

$param = new SoapVar(
    array(
        '_' => 'value',
        'attr' => 'some attribute'
    ), 
    SOME_ENCODING,
    null,
    null,
    null,
    '/some/ns'
);
A: 

SOAP does not support attributes, may be you should use REST instead!

EDIT: Please check the body style w3c:"4.3 SOAP Body" and remember that you need to encode your message with "soap-envelope" namespace and describe your XML types thats why, you can't use attributes to describe your message data.

But if you ask me, it can be made possible! You can use a custom SoapClient parser or something like that and convert your message as you like it. A example of that may be RSS over SOAP http://www.ibm.com/developerworks/webservices/library/ws-soaprdf. But, the problem would be that you would miss the descriptive information about your message data/types and other clients could not easy understand your messages!

My best practice for you would be to use elements instead of attributes, i know you need to fix your XML schema but thats the way it goes or switch to a other technology.

RedAssBaboon
i understand your liking of REST over SOAP. yet this answer really does not help at all. it does not help to tell people, not to use a technology, when they try to solve a problem in that particular technology. plus, what you write is simlpy wrong: SOAP does support attributes. c.f. http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383492
Pierre Spring
No i prefer SOAP over REST. I'm developing with SOAP and PHP webservices for a couple of years now. I've modified my answer a bit, i hope this helps.
RedAssBaboon
RAB's response is technically correct. The problem is that some implementers will require data be passed in attributes anyway. PHP's SoapVar implementation on the other hand follows the letter of the law and doesn't help out with attributes like a normal XML library would.
method