0.0.91 • Published 4 years ago

xpath-stream-empty-mod v0.0.91

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

xpath-stream

Added Empty event handler for checking if the stream is empty or not Now when you pipe xpath-stream with the redable stream object, it will emit 'empty' event if the steam is undefined or null. This is usefull in case where the stream is empty due to no xpath result, the data event is never triggered.

xpath transform stream

Install

$ npm install xpath-stream

Usage

var xpathStream = require('xpath-stream');

// string stream
var Readable = require('stream').Readable;
function testStream(str){
  var stream = new Readable({objectMode:true});
  stream.push(str);
  stream.push(null);
  return stream
};

var xml = testStream([
'<items>',
  '<item id="1">',
  '<name type="001">item1</name>',
  '<price>5000</price>',
  '<nest>',
  '<data>nested</data>',
  '<nest2><data>nested2</data></nest2>',
  '</nest>',
  '</item>',
  '<item id="2"><name type="002">item2</name><price>1000</price></item>',
'</items>'].join(''));

// text node
xml
  .pipe(xpathStream("//item/name/text()"))
  .on('data',console.log);

// attribute value
xml
  .pipe(xpathStream("//item/@id"))
  .on('data',console.log);

// xmldom object
xml
  .pipe(xpathStream("//item"))
  .on('data',console.log);

// object
xml.pipe(xpathStream("//item",{
  id: "./@id",
  type: "name/@type",
  name: "name/text()",
  price: "price/text()",
  nest: {
    data: "nest/data/text()",
    data2: "nest/nest2/data/text()",
    deepNest: {
      deepData: "nest/nest2/data/text()"
    }
  }
}))
.on('data',function(node){
  console.log(JSON.stringify(node));
});

Bonus

$ npm install -g xpath-stream

then

$ xpath-stream "//root/node()" < test.xml

or

$ cat test.xml | xpath-stream "//articles/article[position()=1]/node()"

with namespace

$ xpath-stream --namespace=ns:http://example.com/ns "//ns:item[@ns:id='5A23']" < ./test/test.xml