gulp-xpath is a gulp plugin to executes a XPath query on a XML document in a stream and returns the matches as a string.
var xpath = require('gulp-xpath');
// simple xpath that outputs all matches in a text file
gulp.src(['./news/*.xml'])
.pipe(xpath("/news-items/news[@category='git']"))
.pipe(concat('git_news.txt'))
.pipe(gulp.dest('./news'));
// defining custom namespaces
gulp.src(['./news/*.xml'])
.pipe(xpath("/news-items/news[@category='git']",{"exsl":"http://exslt.org/common"}))
.pipe(concat('git_news.txt'))
.pipe(gulp.dest('./news'));
// defining custom function
gulp.src(['./news/*.xml'])
.pipe(xpath("/news-items/news/@category",{},function(node){
return '<cat>' + node.toString() + '</cat>';
}))
.pipe(concat('all_categories.txt'))
.pipe(gulp.dest('./news'));
- Frist parameter: string
XPath query
- Second parameter: object
namespace declarations
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
is the default namespace (can't be overwritten)
- Third parameter (new): function
customFunction(node)
- This optional custom function gets executed on each matched node. Default return value is
node.toString()
.
- This optional custom function gets executed on each matched node. Default return value is
- To output an valid XML document, I suggest to use gulp-wrapper.