1.2.1 • Published 6 months ago

gulp-xpath v1.2.1

Weekly downloads
635
License
MIT
Repository
github
Last release
6 months ago

gulp-xpath

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.

Usage

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'));

Notes

  • 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().
  • To output an valid XML document, I suggest to use gulp-wrapper.

License

MIT License