Permalink
Browse files

Correction of the use case: no namespace and xpath find

  • Loading branch information...
1 parent 47fb4c0 commit 7735c2b52e93820ce4a6f7ff42c4c8ac696d8779 @VirgileD VirgileD committed Sep 20, 2011
Showing with 15 additions and 2 deletions.
  1. +5 −2 lib/libxml-to-js.js
  2. +10 −0 tests/test-text.js
View
@@ -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);
View
@@ -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.