Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force namespace prefix usage?

I'm trying to consume a SOAP Webservice, but the WSDL is kind of broken, so I have to do some customization to node-soap.

The ideal SOAP Envelope that I would like to have would be this one:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <getImagesDefinition xmlns="http://services.example.com/"/>
    </Body>
</Envelope>

So far this is the nodejs code I have to invoke the service:

var soap = require('soap');
var url = 'http://www.example.com/services/imagesizes?wsdl';

soap.createClient(url, function(err, client) {

    client.setEndpoint('http://www.example.com/services/imagesizes');
    client.getImagesDefinition(null, function(err, result) {
        console.log(result);
    });
    console.log(client.lastRequest)

}); 

I had to set the endpoint manually because it is broken in the WSDL file

The envelope I get when printing client.lastRequest is this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
               xmlns:tns="http://services.example.com/">
    <soap:Body>
        <getImagesDefinition />
    </soap:Body>
</soap:Envelope>

I know that if I can force the namespace prefix on the body to have <tns:getImagesDefinition /> instead of <getImagesDefinition /> the request works perfectly.

Is there any way for me to force it?

I read the documentation saying that tns is a default ignored namespace, so I tried to change that by doing this:

var options = {
    ignoredNamespaces: {
        namespaces: [],
        override: true
    }
}

and sending that object to the soap.createClient method, but I see no difference on the Envelope.

Is there anyway for me to force this? or get to the ideal SOAP Envelope?

Thanks!

like image 344
Rodrigo Sasaki Avatar asked Nov 19 '14 13:11

Rodrigo Sasaki


People also ask

Why do we need to control namespace prefixes?

This is a common reason for controlling namespace prefixes. Another reason is that you want users to edit the XML document manually, and you want to create namespace prefixes that are convenient for the user to type. For example, you might be generating an XSD document.

How do I add a namespace prefix to a schema?

Conventions for schemas suggest that you use either xs or xsd as the prefix for the schema namespace. To control namespace prefixes, you insert attributes that declare namespaces. If you declare the namespaces with specific prefixes, LINQ to XML will attempt to honor the namespace prefixes when serializing.

Do I need to control namespace prefixes when serializing an XML tree?

This article describes how to control namespace prefixes when serializing an XML tree in C# and Visual Basic. In many situations, it's not necessary to control namespace prefixes. However, certain XML programming tools require it.

How do I use prefixes in LINQ to XML?

If you declare the namespaces with specific prefixes, LINQ to XML will attempt to honor the namespace prefixes when serializing. To create an attribute that declares a namespace with a prefix, you create an attribute where the namespace of the name of the attribute is Xmlns, and the name of the attribute is the namespace prefix.


2 Answers

I ran into this exact problem and for me, the fix was to override the ignoredNamespaces - to remove 'tns' as an ignored namespace.

var options = { 
  ignoredNamespaces: {
    namespaces: ['targetNamespace', 'typedNamespace'],
    override: true
  }
}

I'm not sure why it didn't work for you, but maybe there was a bug in the library that has since been fixed. Or maybe because you didn't include any namespaces, but rather an empty array.

like image 142
Halfstop Avatar answered Oct 20 '22 23:10

Halfstop


See this thread discussing the same issue at github:

And especially https://github.com/vpulim/node-soap/issues/537#issuecomment-72041420

like image 3
Simon Avatar answered Oct 21 '22 00:10

Simon