Skip to content

Gulp module that executes a XPath query on a XML document in a stream and returns the matches as a string.

License

Notifications You must be signed in to change notification settings

donum/gulp-xpath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Gulp module that executes a XPath query on a XML document in a stream and returns the matches as a string.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published