XPath expression evaluator against a XML-SAX stream
npm install saxpathSaXPath
=======
Simple XPath evaluator which runs against a SAX stream. 
Supported XPath construct as of writing are:
- '/'-axis (child)
- '//'-axis (self-or-descendant)
- node name tests, including namespaces
- all nodes selector: '*'
- predicate test:
- @attribute_name = "literal"
Usage
-----
Instantiate a new SaXPath object with new saxpath.SaXPath(saxParser, xpath [, recorder]). Then pipe a stream into it and SaXPath will emit match events on each XPath match. The emitted data is managed by the recorder, which by default recreates on the fly as a (XML) string from the SAX events.
Example
-------
An example of how to use this library is as follows:
``
var saxpath = require('./lib');
var fs = require('fs');
var sax = require('sax');
var fileStream = fs.createReadStream('test/bookstore.xml');
var saxParser = sax.createStream(true);
var streamer = new saxpath.SaXPath(saxParser, '//book');
streamer.on('match', function(xml) {
console.log('--- matched XML ---');
console.log(xml);
});
fileStream.pipe(saxParser);
`
In the example, the expression `//book` is evaluated against the file `/test/bookstore.xml`. The result is that all books are returned in the function. The output of the script is:
``
--- matched XML ---
--- matched XML ---
--- matched XML ---
--- matched XML ---
Check out the examples directory for usage examples.
Inner workings
--------------
A state machine is built which the SAX-events are tested against. If an event matches, the state machine progresses.
For self-or-descendant-nodes, the state machine is forked and earch fork (including the parent) is tested against the SAX-nodes. This ensures all nodes are matched. See test/saxpath.js and test/inception.xml for an example.
Each SAX event emitted by the saxParser is transmitted to a recorder, which is in charge of handling the data if it matches the XPath. Check examples/custom-recorder.js` to see how to create a custom recorder.