Skip to content
This repository
Browse code

Add xmlns flag, to enable adding namespace information to elements

  • Loading branch information...
commit b49f9655ef068f4b8ee5833f30a5eb79740fcf18 1 parent b067c88
Mike Schilling mikeatemotive authored
3  README.md
Source Rendered
@@ -134,6 +134,9 @@ value})``. Possible options are:
134 134 * `validator` (default `null`): You can specify a callable that validates
135 135 the resulting structure somehow, however you want. See unit tests
136 136 for an example.
  137 + * `xmlns` (default `false`): Give each element a field usually called '$ns'
  138 + (the first character is the same as attrkey) that contains its local name
  139 + and namespace uri.
137 140
138 141 These default settings are for backward-compatibility. These are scheduled to
139 142 change to a more 'clean' way of parsing in version 0.2.
18 lib/xml2js.js
@@ -25,7 +25,8 @@
25 25 ignoreAttrs: false,
26 26 mergeAttrs: false,
27 27 explicitRoot: false,
28   - validator: null
  28 + validator: null,
  29 + xmlns: false
29 30 },
30 31 "0.2": {
31 32 explicitCharkey: false,
@@ -38,7 +39,8 @@
38 39 ignoreAttrs: false,
39 40 mergeAttrs: false,
40 41 explicitRoot: true,
41   - validator: null
  42 + validator: null,
  43 + xmlns: false
42 44 }
43 45 };
44 46
@@ -76,6 +78,9 @@
76 78 value = opts[key];
77 79 this.options[key] = value;
78 80 }
  81 + if (this.options.xmlns) {
  82 + this.options.xmlnskey = this.options.attrkey + "ns";
  83 + }
79 84 this.reset();
80 85 }
81 86
@@ -85,7 +90,8 @@
85 90 this.removeAllListeners();
86 91 this.saxParser = sax.parser(true, {
87 92 trim: false,
88   - normalize: false
  93 + normalize: false,
  94 + xmlns: this.options.xmlns
89 95 });
90 96 err = false;
91 97 this.saxParser.onerror = function(error) {
@@ -118,6 +124,12 @@
118 124 }
119 125 }
120 126 obj["#name"] = _this.options.normalizeTags ? node.name.toLowerCase() : node.name;
  127 + if (_this.options.xmlns) {
  128 + obj[_this.options.xmlnskey] = {
  129 + uri: node.uri,
  130 + local: node.local
  131 + };
  132 + }
121 133 return stack.push(obj);
122 134 };
123 135 this.saxParser.onclosetag = function() {
16 src/xml2js.coffee
@@ -25,7 +25,8 @@ exports.defaults =
25 25 # cause collisions.
26 26 mergeAttrs: false
27 27 explicitRoot: false
28   - validator: null
  28 + validator: null,
  29 + xmlns : false
29 30 "0.2":
30 31 explicitCharkey: false
31 32 trim: false
@@ -37,7 +38,8 @@ exports.defaults =
37 38 ignoreAttrs: false
38 39 mergeAttrs: false
39 40 explicitRoot: true
40   - validator: null
  41 + validator: null,
  42 + xmlns : false
41 43
42 44 class exports.ValidationError extends Error
43 45 constructor: (message) ->
@@ -50,7 +52,10 @@ class exports.Parser extends events.EventEmitter
50 52 @options[key] = value for own key, value of exports.defaults["0.2"]
51 53 # overwrite them with the specified options, if any
52 54 @options[key] = value for own key, value of opts
53   -
  55 + # define the key used for namespaces
  56 + if @options.xmlns
  57 + @options.xmlnskey = @options.attrkey + "ns"
  58 +
54 59 @reset()
55 60
56 61 reset: =>
@@ -61,7 +66,8 @@ class exports.Parser extends events.EventEmitter
61 66 # very helpful
62 67 @saxParser = sax.parser true, {
63 68 trim: false,
64   - normalize: false
  69 + normalize: false,
  70 + xmlns: @options.xmlns
65 71 }
66 72
67 73 # emit one error event if the sax parser fails. this is mostly a hack, but
@@ -96,6 +102,8 @@ class exports.Parser extends events.EventEmitter
96 102
97 103 # need a place to store the node name
98 104 obj["#name"] = if @options.normalizeTags then node.name.toLowerCase() else node.name
  105 + if (@options.xmlns)
  106 + obj[@options.xmlnskey] = {uri: node.uri, local : node.local}
99 107 stack.push obj
100 108
101 109 @saxParser.onclosetag = =>
3  test/fixtures/sample.xml
@@ -44,4 +44,7 @@
44 44 <numbertest>42</numbertest>
45 45 <stringtest>43</stringtest>
46 46 </validatortest>
  47 + <pfx:top xmlns:pfx="http://foo.com" pfx:attr="baz">
  48 + <middle xmlns="http://bar.com"/>
  49 + </pfx:top>
47 50 </sample>
10 test/xml2js.test.coffee
@@ -206,3 +206,13 @@ module.exports =
206 206 x2js.parseString '<validationerror/>', (err, r) ->
207 207 assert.equal err, 'Validation error!'
208 208 test.finish()
  209 +
  210 + 'test xmlns': skeleton(xmlns: true, (r) ->
  211 + console.log 'Result object: ' + util.inspect r, false, 10
  212 + equ r.sample["pfx:top"][0].$ns.local, 'top'
  213 + equ r.sample["pfx:top"][0].$ns.uri, 'http://foo.com'
  214 + equ r.sample["pfx:top"][0].$["pfx:attr"].value, 'baz'
  215 + equ r.sample["pfx:top"][0].$["pfx:attr"].local, 'attr'
  216 + equ r.sample["pfx:top"][0].$["pfx:attr"].uri, 'http://foo.com'
  217 + equ r.sample["pfx:top"][0].middle[0].$ns.local, 'middle'
  218 + equ r.sample["pfx:top"][0].middle[0].$ns.uri, 'http://bar.com')

0 comments on commit b49f965

Please sign in to comment.
Something went wrong with that request. Please try again.