コマンドラインツールを自作するのはどうでしょうか。
以下のコードは npm i && npm start
で実行できます。
find
の後のドット(.)は、検索したいディレクトリを指定してください。
簡易的なものなので検索条件が固定で
ファイルパスと検索にかかったコードしか表示していませんが、
工夫次第でもっと使いやすくもできるかと思います。
javascript は、 xpath.js というファイル名で保存
json は、 package.json というファイル名で保存してください。
また、実行には node.js が必要になります。
js
1const program = require('commander');
2const fs = require('fs');
3const parse5 = require('parse5');
4const dom = require('xmldom').DOMParser;
5const xpath = require('xpath');
6
7program.parse(process.argv);
8let filePaths = program.args;
9
10for (let filePath of filePaths) {
11 fs.readFile(filePath, (err, file) => {
12 let document = parse5.parse(file.toString());
13 let html = parse5.serialize(document);
14 let doc = new dom({ errorHandler: false }).parseFromString(html, 'text/html');
15 let select = xpath.useNamespaces({ 'x': 'http://www.w3.org/1999/xhtml' });
16
17 let nodes = select('//x:*[contains(@class,"class15")]/descendant-or-self::node()/descendant::*[name()="span"]', doc);
18 for (let node of nodes) {
19 console.log(filePath + ': ' + node);
20 }
21 });
22}
json
1{
2 "name": "xpath",
3 "version": "1.0.0",
4 "description": "",
5 "main": "xpath.js",
6 "scripts": {
7 "start": "find . -type f -name '*.html' | xargs node xpath.js"
8 },
9 "keywords": [],
10 "author": "",
11 "license": "ISC",
12 "devDependencies": {
13 "commander": "^2.20.0",
14 "parse5": "^5.1.0",
15 "xmldom": "^0.1.27",
16 "xpath": "0.0.27"
17 }
18}