Skip to content

Commit

Permalink
Correction of the use case: no namespace and xpath find
Browse files Browse the repository at this point in the history
  • Loading branch information
VirgileD committed Sep 20, 2011
1 parent 47fb4c0 commit 7735c2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/libxml-to-js.js
Expand Up @@ -148,12 +148,15 @@ module.exports = function (xml, xpath, callback) {
callback = xpath;
xpath = null;
}
var xmlDocument, jsDocument, selected = [];
var xmlDocument, jsDocument, selected = [], xmlns = null;
try {
xmlDocument = libxmljs.parseXmlString(xml);
jsDocument = libxml2js(xmlDocument);
if( jsDocument['@'] && jsDocument['@'].xmlns) {
xmlns = jsDocument['@'].xmlns;
}
if( !! xpath) {
xmlDocument.find(xpath, jsDocument['@'].xmlns).forEach(function(item) {
xmlDocument.find(xpath, xmlns).forEach(function(item) {
selected.push(libxml2js(item, true).jsobj);
});
return callback(null, selected);
Expand Down
10 changes: 10 additions & 0 deletions tests/test-text.js
Expand Up @@ -34,3 +34,13 @@ parser(fs.readFileSync('data/text.xml').toString(), function (err, res) {
},
res);
});

parser(fs.readFileSync('data/text.xml').toString(), '//nouvelle/news', function (err, res) {
assert.ifError(err);
assert.deepEqual({
"auteur": "Bizzard5",
"date": "17 Août 2008",
"text": {}
},
res[0]);
});

0 comments on commit 7735c2b

Please sign in to comment.