Skip to content

Commit f359300

Browse files
Damien PicardValko54
authored andcommitted
Define $xml to pass xml object
Add unit test to $xml key with request Update readme.md
1 parent 3be4afa commit f359300

File tree

9 files changed

+156
-4
lines changed

9 files changed

+156
-4
lines changed

Readme.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,18 @@ WSSecurity implements WS-Security. UsernameToken and PasswordText/PasswordDiges
214214
//'PasswordDigest' or 'PasswordText' default is PasswordText
215215
```
216216

217-
## Handling XML Attributes and Value (wsdlOptions).
217+
## Handling XML Attributes, Value and XML (wsdlOptions).
218218
Sometimes it is necessary to override the default behaviour of `node-soap` in order to deal with the special requirements
219219
of your code base or a third library you use. Therefore you can use the `wsdlOptions` Object, which is passed in the
220220
`#createClient()` method and could have any (or all) of the following contents:
221221
```javascript
222222
var wsdlOptions = {
223223
attributesKey: 'theAttrs',
224-
valueKey: 'theVal'
224+
valueKey: 'theVal',
225+
xmlKey: 'theXml'
225226
}
226227
```
227-
If nothing (or an empty Object `{}`) is passed to the `#createClient()` method, the `node-soap` defaults (`attributesKey: 'attributes'`
228-
and `valueKey: '$value'`) are used.
228+
If nothing (or an empty Object `{}`) is passed to the `#createClient()` method, the `node-soap` defaults (`attributesKey: 'attributes'`, `valueKey: '$value'` and `xmlKey: '$xml'`) are used.
229229

230230
###Overriding the `value` key
231231
By default, `node-soap` uses `$value` as key for any parsed XML value which may interfere with your other code as it
@@ -242,6 +242,36 @@ soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', wsdlOptions, funct
242242
});
243243
```
244244

245+
###Overriding the `xml` key
246+
As `valueKey`, `node-soap` uses `$xml` as key. The xml key is used to pass XML Object without adding namespace or parsing the string.
247+
248+
Example :
249+
250+
```javascript
251+
dom = {
252+
$xml: '<parentnode type="type"><childnode></childnode></parentnode>'
253+
};
254+
```
255+
256+
```xml
257+
<tns:dom>
258+
<parentnode type="type">
259+
<childnode></childnode>
260+
</parentnode>
261+
</tns:dom>
262+
```
263+
264+
You can define your own `xmlKey` by passing it in the `wsdl_options` to the createClient call like so:
265+
```javascript
266+
var wsdlOptions = {
267+
xmlKey: 'theXml'
268+
};
269+
270+
soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', wsdlOptions, function (err, client) {
271+
// your code
272+
});
273+
```
274+
245275
###Overriding the `attributes` key
246276
You can achieve attributes like:
247277
``` xml

lib/wsdl.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ var Element = function(nsName, attrs, options) {
123123
Element.prototype._initializeOptions = function (options) {
124124
if(options) {
125125
this.valueKey = options.valueKey || '$value';
126+
this.xmlKey = options.xmlKey || '$xml';
126127
this.ignoredNamespaces = options.ignoredNamespaces || [];
127128
} else {
128129
this.valueKey = '$value';
130+
this.xmlKey = '$xml';
129131
this.ignoredNamespaces = [];
130132
}
131133
};
@@ -1030,6 +1032,7 @@ var WSDL = function(definition, uri, options) {
10301032
WSDL.prototype.ignoredNamespaces = ['tns', 'targetNamespace', 'typedNamespace'];
10311033

10321034
WSDL.prototype.valueKey = '$value';
1035+
WSDL.prototype.xmlKey = '$xml';
10331036

10341037
WSDL.prototype._initializeOptions = function (options) {
10351038
this.options = {};
@@ -1048,6 +1051,7 @@ WSDL.prototype._initializeOptions = function (options) {
10481051
}
10491052

10501053
this.options.valueKey = options.valueKey || this.valueKey;
1054+
this.options.xmlKey = options.xmlKey || this.xmlKey;
10511055
};
10521056

10531057
WSDL.prototype.onReady = function(callback) {
@@ -1415,6 +1419,10 @@ WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, first, xmlnsA
14151419
if (name === self.options.attributesKey) {
14161420
continue;
14171421
}
1422+
//Its the value of a xml object. Return it directly.
1423+
if (name === self.options.xmlKey){
1424+
return obj[name];
1425+
}
14181426
//Its the value of an item. Return it directly.
14191427
if (name === self.options.valueKey) {
14201428
return xmlEscape(obj[name]);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:tns="http://www.Dummy.com/Common/Types" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.Dummy.com/Common/Types" elementFormDefault="qualified" attributeFormDefault="unqualified">
3+
<xs:complexType name="DummyResult">
4+
<xs:sequence>
5+
<xs:element name="DummyList" type="tns:DummyList" minOccurs="0"/>
6+
</xs:sequence>
7+
<xs:attribute name="code" type="xs:string" use="optional"/>
8+
</xs:complexType>
9+
<xs:complexType name="Dummy">
10+
<xs:simpleContent>
11+
<xs:extension base="xs:string">
12+
<xs:attribute name="language" type="xs:language" use="optional"/>
13+
</xs:extension>
14+
</xs:simpleContent>
15+
</xs:complexType>
16+
<xs:complexType name="DummyList">
17+
<xs:sequence>
18+
<xs:element name="DummyElement" type="tns:Dummy" maxOccurs="unbounded"/>
19+
</xs:sequence>
20+
</xs:complexType>
21+
</xs:schema>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:tns="http://www.Dummy.com/Name/Types" xmlns:xs="http://www.w3.org/2001/XMLSchema"
3+
targetNamespace="http://www.Dummy.com/Name/Types" elementFormDefault="qualified"
4+
attributeFormDefault="unqualified" xmlns:c="http://www.Dummy.com/Common/Types">
5+
<xs:import namespace="common.xsd" schemaLocation="common.xsd"/>
6+
<xs:element name="DummyRequest">
7+
<xs:complexType>
8+
<xs:sequence>
9+
<xs:element name="DummyXML" type="xs:string" minOccurs="0"/>
10+
</xs:sequence>
11+
</xs:complexType>
12+
</xs:element>
13+
<xs:element name="DummyResponse">
14+
<xs:complexType>
15+
<xs:sequence>
16+
<xs:element name="DummyResult" type="c:DummyResult"/>
17+
</xs:sequence>
18+
</xs:complexType>
19+
</xs:element>
20+
</xs:schema>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"DummyXML": {
3+
"$xml": "<myObject attr='myAttr'></myObject>"
4+
}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tns="http://www.Dummy.com" xmlns:n="http://www.Dummy.com/Name/Types"><soap:Body><n:DummyRequest xmlns:n="http://www.Dummy.com/Name/Types" xmlns="http://www.Dummy.com/Name/Types"><n:DummyXML><myObject attr='myAttr'></myObject></n:DummyXML></n:DummyRequest></soap:Body></soap:Envelope>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"DummyResult": {
3+
"DummyList": {
4+
"DummyElement": {
5+
"attributes": {
6+
"language": "en-US"
7+
},
8+
"$value": "Dummy Element Entry"
9+
}
10+
}
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.Dummy.com" xmlns:n="http://www.Dummy.com/Name/Types">
2+
<soap:Header></soap:Header>
3+
<soap:Body>
4+
<n:DummyResponse>
5+
<n:DummyResult>
6+
<c:DummyList xmlns:c="http://www.Dummy.com/Common/Types">
7+
<c:DummyElement language="en-US">
8+
Dummy Element Entry
9+
</c:DummyElement>
10+
</c:DummyList>
11+
</n:DummyResult>
12+
</n:DummyResponse>
13+
</soap:Body>
14+
</soap:Envelope>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
3+
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
4+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
5+
xmlns:tns="http://www.Dummy.com" xmlns:n="http://www.Dummy.com/Name/Types" xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://www.Dummy.com">
6+
<wsdl:types>
7+
<xs:schema>
8+
<xs:import namespace="http://www.Dummy.com/Common/Types" schemaLocation="common.xsd"/>
9+
<xs:import namespace="http://www.Dummy.com/Name/Types" schemaLocation="name.xsd"/>
10+
</xs:schema>
11+
</wsdl:types>
12+
<wsdl:message name="DummyRequest">
13+
<wsdl:part name="DummyRequest" element="n:DummyRequest"/>
14+
</wsdl:message>
15+
<wsdl:message name="DummyResponse">
16+
<wsdl:part name="DummyResponse" element="n:DummyResponse"/>
17+
</wsdl:message>
18+
<wsdl:portType name="DummyPortType">
19+
<wsdl:operation name="Dummy">
20+
<wsdl:input message="tns:DummyRequest"/>
21+
<wsdl:output message="tns:DummyResponse"/>
22+
</wsdl:operation>
23+
</wsdl:portType>
24+
<wsdl:binding name="DummyBinding" type="tns:DummyPortType">
25+
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
26+
<wsdl:operation name="Dummy">
27+
<soap:operation soapAction="http://www.Dummy.com#Dummy" style="document"/>
28+
<wsdl:input>
29+
<soap:body use="literal"/>
30+
</wsdl:input>
31+
<wsdl:output>
32+
<soap:body use="literal"/>
33+
</wsdl:output>
34+
</wsdl:operation>
35+
</wsdl:binding>
36+
<wsdl:service name="DummyService">
37+
<wsdl:port name="DummyPortType" binding="tns:DummyBinding">
38+
<soap:address location="http://www.Dummy.com/"/>
39+
</wsdl:port>
40+
</wsdl:service>
41+
</wsdl:definitions>

0 commit comments

Comments
 (0)