Skip to content
Browse files

Add xmlns flag, to enable adding namespace information to elements

  • Loading branch information...
1 parent b067c88 commit b49f9655ef068f4b8ee5833f30a5eb79740fcf18 @mikeatemotive mikeatemotive committed Nov 15, 2012
Showing with 43 additions and 7 deletions.
  1. +3 −0 README.md
  2. +15 −3 lib/xml2js.js
  3. +12 −4 src/xml2js.coffee
  4. +3 −0 test/fixtures/sample.xml
  5. +10 −0 test/xml2js.test.coffee
View
3 README.md
@@ -134,6 +134,9 @@ value})``. Possible options are:
* `validator` (default `null`): You can specify a callable that validates
the resulting structure somehow, however you want. See unit tests
for an example.
+ * `xmlns` (default `false`): Give each element a field usually called '$ns'
+ (the first character is the same as attrkey) that contains its local name
+ and namespace uri.
These default settings are for backward-compatibility. These are scheduled to
change to a more 'clean' way of parsing in version 0.2.
View
18 lib/xml2js.js
@@ -25,7 +25,8 @@
ignoreAttrs: false,
mergeAttrs: false,
explicitRoot: false,
- validator: null
+ validator: null,
+ xmlns: false
},
"0.2": {
explicitCharkey: false,
@@ -38,7 +39,8 @@
ignoreAttrs: false,
mergeAttrs: false,
explicitRoot: true,
- validator: null
+ validator: null,
+ xmlns: false
}
};
@@ -76,6 +78,9 @@
value = opts[key];
this.options[key] = value;
}
+ if (this.options.xmlns) {
+ this.options.xmlnskey = this.options.attrkey + "ns";
+ }
this.reset();
}
@@ -85,7 +90,8 @@
this.removeAllListeners();
this.saxParser = sax.parser(true, {
trim: false,
- normalize: false
+ normalize: false,
+ xmlns: this.options.xmlns
});
err = false;
this.saxParser.onerror = function(error) {
@@ -118,6 +124,12 @@
}
}
obj["#name"] = _this.options.normalizeTags ? node.name.toLowerCase() : node.name;
+ if (_this.options.xmlns) {
+ obj[_this.options.xmlnskey] = {
+ uri: node.uri,
+ local: node.local
+ };
+ }
return stack.push(obj);
};
this.saxParser.onclosetag = function() {
View
16 src/xml2js.coffee
@@ -25,7 +25,8 @@ exports.defaults =
# cause collisions.
mergeAttrs: false
explicitRoot: false
- validator: null
+ validator: null,
+ xmlns : false
"0.2":
explicitCharkey: false
trim: false
@@ -37,7 +38,8 @@ exports.defaults =
ignoreAttrs: false
mergeAttrs: false
explicitRoot: true
- validator: null
+ validator: null,
+ xmlns : false
class exports.ValidationError extends Error
constructor: (message) ->
@@ -50,7 +52,10 @@ class exports.Parser extends events.EventEmitter
@options[key] = value for own key, value of exports.defaults["0.2"]
# overwrite them with the specified options, if any
@options[key] = value for own key, value of opts
-
+ # define the key used for namespaces
+ if @options.xmlns
+ @options.xmlnskey = @options.attrkey + "ns"
+
@reset()
reset: =>
@@ -61,7 +66,8 @@ class exports.Parser extends events.EventEmitter
# very helpful
@saxParser = sax.parser true, {
trim: false,
- normalize: false
+ normalize: false,
+ xmlns: @options.xmlns
}
# 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
# need a place to store the node name
obj["#name"] = if @options.normalizeTags then node.name.toLowerCase() else node.name
+ if (@options.xmlns)
+ obj[@options.xmlnskey] = {uri: node.uri, local : node.local}
stack.push obj
@saxParser.onclosetag = =>
View
3 test/fixtures/sample.xml
@@ -44,4 +44,7 @@
<numbertest>42</numbertest>
<stringtest>43</stringtest>
</validatortest>
+ <pfx:top xmlns:pfx="http://foo.com" pfx:attr="baz">
+ <middle xmlns="http://bar.com"/>
+ </pfx:top>
</sample>
View
10 test/xml2js.test.coffee
@@ -206,3 +206,13 @@ module.exports =
x2js.parseString '<validationerror/>', (err, r) ->
assert.equal err, 'Validation error!'
test.finish()
+
+ 'test xmlns': skeleton(xmlns: true, (r) ->
+ console.log 'Result object: ' + util.inspect r, false, 10
+ equ r.sample["pfx:top"][0].$ns.local, 'top'
+ equ r.sample["pfx:top"][0].$ns.uri, 'http://foo.com'
+ equ r.sample["pfx:top"][0].$["pfx:attr"].value, 'baz'
+ equ r.sample["pfx:top"][0].$["pfx:attr"].local, 'attr'
+ equ r.sample["pfx:top"][0].$["pfx:attr"].uri, 'http://foo.com'
+ equ r.sample["pfx:top"][0].middle[0].$ns.local, 'middle'
+ 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.