Skip to content
Newer
Older
100644 85 lines (70 sloc) 2.92 KB
6101e44 @StevenLooman Initial commit
authored Aug 13, 2012
1 SaXPath
2 =======
7ee9f0f @StevenLooman Enabling Travis-CI
authored Aug 20, 2012
3 Simple XPath evaluator which runs against a SAX stream. [![Build Status](https://secure.travis-ci.org/StevenLooman/saxpath.png)](http://travis-ci.org/StevenLooman/saxpath)
5a807f1 @StevenLooman README file
authored Aug 19, 2012
4
5 Supported XPath construct as of writing are:
6 - '/'-axis (child)
7 - '//'-axis (self-or-descendant)
2cab597 @PaulMougel Updated wildcard definition in README
PaulMougel authored Jul 22, 2013
8 - node name tests, including namespaces
9 - all nodes selector: '*'
7321a66 @StevenLooman Fixed some typos and minor improvements
authored Nov 3, 2013
10 - predicate test:
90600c3 @StevenLooman Make names appear
authored Nov 3, 2013
11 - @attribute_name = "literal"
5a807f1 @StevenLooman README file
authored Aug 19, 2012
12
b6be076 @PaulMougel Updated documentation and added a custom recorder example
PaulMougel authored Aug 30, 2013
13 Usage
14 -----
7321a66 @StevenLooman Fixed some typos and minor improvements
authored Nov 3, 2013
15 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.
b6be076 @PaulMougel Updated documentation and added a custom recorder example
PaulMougel authored Aug 30, 2013
16
381f1c9 @StevenLooman Added example to readme. Refs #5
authored Aug 30, 2013
17 Example
18 -------
19
20 An example of how to use this library is as follows:
21
22 ```
23 var saxpath = require('./lib');
24 var fs = require('fs');
25 var sax = require('sax');
26
27 var fileStream = fs.createReadStream('test/bookstore.xml');
28 var saxParser = sax.createStream(true);
29 var streamer = new saxpath.SaXPath(saxParser, '//book');
30
31 streamer.on('match', function(xml) {
32 console.log('--- matched XML ---');
33 console.log(xml);
34 });
35
36 fileStream.pipe(saxParser);
37 ```
38
39 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:
40
41 ```
42 --- matched XML ---
43 <book category="COOKING">
44 <title lang="en">Everyday Italian</title>
45 <author>Giada De Laurentiis</author>
46 <year>2005</year>
47 <price>30.00</price>
48 </book>
49 --- matched XML ---
50 <book category="CHILDREN">
51 <title lang="en">Harry Potter</title>
52 <author>J K. Rowling</author>
53 <year>2005</year>
54 <price>29.99</price>
55 </book>
56 --- matched XML ---
57 <book category="WEB">
58 <title lang="en">XQuery Kick Start</title>
59 <author>James McGovern</author>
60 <author>Per Bothner</author>
61 <author>Kurt Cagle</author>
62 <author>James Linn</author>
63 <author>Vaidyanathan Nagarajan</author>
64 <year>2003</year>
65 <price>49.99</price>
66 </book>
67 --- matched XML ---
68 <book category="WEB">
69 <title lang="en">Learning XML</title>
70 <author>Erik T. Ray</author>
71 <year>2003</year>
72 <price>39.95</price>
73 </book>
74 ```
75
b6be076 @PaulMougel Updated documentation and added a custom recorder example
PaulMougel authored Aug 30, 2013
76 Check out the `examples` directory for usage examples.
77
5a807f1 @StevenLooman README file
authored Aug 19, 2012
78 Inner workings
79 --------------
7321a66 @StevenLooman Fixed some typos and minor improvements
authored Nov 3, 2013
80 A state machine is built which the SAX-events are tested against. If an event matches, the state machine progresses.
5a807f1 @StevenLooman README file
authored Aug 19, 2012
81
b6be076 @PaulMougel Updated documentation and added a custom recorder example
PaulMougel authored Aug 30, 2013
82 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.
83
7321a66 @StevenLooman Fixed some typos and minor improvements
authored Nov 3, 2013
84 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.
Something went wrong with that request. Please try again.