Skip to content

Commit

Permalink
Added stripPrefix option
Browse files Browse the repository at this point in the history
  • Loading branch information
creynders committed Dec 12, 2013
1 parent 23b4776 commit 8657e29
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ value})``. Possible options are:
* `strict` (default `true`): Set sax-js to strict or non-strict parsing mode.
Defaults to `true` which is *highly* recommended, since parsing HTML which
is not well-formed XML might yield just about anything. Added in 0.2.7.
* `stripPrefix` (default `false`): Strip namespace prefixes from element and
attribute names. Added in 0.4.1.

Options for the `Builder` class
-------------------------------
Expand Down
2 changes: 1 addition & 1 deletion lib/bom.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 24 additions & 7 deletions lib/xml2js.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/xml2js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ exports.defaults =
# callbacks are async? not in 0.1 mode
async: false
strict: true
stripPrefix: false

"0.2":
explicitCharkey: false
Expand All @@ -56,6 +57,7 @@ exports.defaults =
# not async in 0.2 mode either
async: false
strict: true
stripPrefix: false
# xml building options
rootName: 'root'
xmldec: {'version': '1.0', 'encoding': 'UTF-8', 'standalone': true}
Expand Down Expand Up @@ -181,22 +183,26 @@ class exports.Parser extends events.EventEmitter
# aliases, so we don't have to type so much
attrkey = @options.attrkey
charkey = @options.charkey
stripPrefix = @options.stripPrefix
prefixMatch = new RegExp /(?!xmlns)^.*:/

@saxParser.onopentag = (node) =>
obj = {}
obj[charkey] = ""
unless @options.ignoreAttrs
for own key of node.attributes
objkey = if @options.stripPrefix then key.replace(prefixMatch, '') else key
if attrkey not of obj and not @options.mergeAttrs
obj[attrkey] = {}
newValue = node.attributes[key]
if @options.mergeAttrs
@assignOrPush obj, key, newValue
@assignOrPush obj, objkey, newValue
else
obj[attrkey][key] = newValue
obj[attrkey][objkey] = newValue

# need a place to store the node name
obj["#name"] = if @options.normalizeTags then node.name.toLowerCase() else node.name
nodeName = if @options.normalizeTags then node.name.toLowerCase() else node.name
obj["#name"] = if @options.stripPrefix then nodeName.replace(prefixMatch, '') else nodeName
if (@options.xmlns)
obj[@options.xmlnskey] = {uri: node.uri, local: node.local}
stack.push obj
Expand Down
6 changes: 6 additions & 0 deletions test/parser.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ module.exports =
equ r.sample["pfx:top"][0].middle[0].$ns.local, 'middle'
equ r.sample["pfx:top"][0].middle[0].$ns.uri, 'http://bar.com')

'test stripPrefix': skeleton(stripPrefix: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample["top"][0].$["attr"], 'baz'
equ r.sample["top"][0].$["xmlns:pfx"], 'http://foo.com'
)

'test callback should be called once': (test) ->
xml = '<?xml version="1.0" encoding="utf-8"?><test>test</test>'
i = 0
Expand Down

0 comments on commit 8657e29

Please sign in to comment.