nodules / xamel

Fast and cozy way to extract data from XML
MIT License
30 stars 5 forks source link

Nested find/$ not working #22

Closed stevenvachon closed 10 years ago

stevenvachon commented 10 years ago
xamel.parse('<data>Answer: %s<number>42</number></data>', function(err, xml) {
    if (!err) {
        console.log( xml.find('data/number/text()') );  // works
        console.log( xml.$('data/number/text()') ); // works

        console.log( xml.find('data').find('number/text()') );  // fails
        console.log( xml.$('data').$('number/text()') );    // fails
    }
});
kaero commented 10 years ago

It's ok ;)

xml.$('data') // => NodeSet[ <data> ]
xml.$('data/*') // => NodeSet[ ..children of the <data> ..]
xml.$('data/*').$('number/text()') // done

NodeSet.$/find start walking from current NodeSet members, not childs, and produce NodeSet of the found nodes.

kaero commented 10 years ago

to get data tag, not the NodeSet of the data tag:

xml.$('data').eq(0).$('number/text()') // same as $('data/*').$('number/text()')
kaero commented 10 years ago

Finally, remember: NodeSet#find always return a NodeSet.