How to use xpath

Comprehensive xpath code examples:

How to use xpath.fromPageSource:

28
29
30
31
32
33
34
35
36
37
var value = values[i];
if(value.status != 200) {
	reject({codigo: 500, descripcion: "Se ha producido un error"});
	return;
}
//const node = xpath.fromPageSource(value.data);
var doc = parse5.parse(value.data);
const xhtml = xmlser.serializeToString(doc);
			const d = new dom().parseFromString(xhtml);
			const select = xpath.useNamespaces({"x": "http://www.w3.org/1999/xhtml"});

How to use xpath.default:

85
86
87
88
89
90
91
92
93
94
const doc = new xmldom_1.DOMParser().parseFromString(temp_response.data);
const contract_addresses = xpath_1.default
    .select('//*[@id="transfers"]/div[2]/table/tbody/tr/td[1]/a/text()', doc)
    .toString()
    .split(",");
const contract_names = xpath_1.default
    .select('//*[@id="transfers"]/div[2]/table/tbody/tr/td[2]/text()', doc)
    .toString()
    .split(",");
for (let i = 0; i < contract_addresses.length; i++) {

How to use xpath.parse:

1
2
3
4
5
6
7
8
9
10
11
const xpath = require('xpath');
const app = express();


app.get('/some/route', function(req, res) {
  let tainted = req.param("userName");
  xpath.parse(tainted); // NOT OK
  xpath.select(tainted); // NOT OK
  xpath.select1(tainted); // NOT OK
  let expr = xpath.useNamespaces(map);
  expr(tainted); // NOT OK

How to use xpath.XBoolean:

33
34
35
36
37
38
39
40
41
42
  functionNameExpr
) {
  const functionName = (typeof functionNameExpr === 'string') ? functionNameExpr : functionNameExpr.evaluate(xPathContext);
  const functionFound = xPathContext.functionResolver.getFunction(functionName) !== undefined;

  return new XPath.XBoolean(functionFound);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*

How to use xpath.select1:

3
4
5
6
7
8
9
10
11
12


app.get('/some/route', function(req, res) {
  let tainted = req.param("userName");
  xpath.parse(tainted); // NOT OK
  xpath.select(tainted); // NOT OK
  xpath.select1(tainted); // NOT OK
  let expr = xpath.useNamespaces(map);
  expr(tainted); // NOT OK
});

How to use xpath.useNamespaces:

6
7
8
9
10
11
12
13
14
15
16
const DOMParser = xmldom.DOMParser;
const zlib = require("zlib");
const path = require("path");
const fs = require("fs");


const select = xpath.useNamespaces({
    ...namespaces, 
    "spid": "https://spid.gov.it/saml-extensions"
});

How to use xpath.evaluate:

336
337
338
339
340
341
342
343
344
345
var serializer = new XMLSerializer();
const doc = docParser.parseFromString(result.data, "application/xml")

try {
    if (entry.xPath && entry.xPath !== "") {
        const allParagraphs = xpath.evaluate(
            entry.xPath,
            doc,
            null,
            xpath.XPathResult.ANY_TYPE,

How to use xpath.XNodeSet:

47
48
49
50
51
52
53
54
55
56
 * @returns {XPath.XNodeSet} - The context node within a node set.
 */
static current (
  xPathContext
) {
  const nodeSet = new XPath.XNodeSet();
  nodeSet.add(xPathContext.contextNode);

  return nodeSet;
}

How to use xpath.XPathResult:

265
266
267
268
269
270
271
272
273
274
const rightSide = match[3];

if ((/^[.$]/).test(xPath) || (/:\/\(/).testXPath) {
  try {
    const context = this.clone({ transformNode: transformNode });
    value = leftSide + this.processWhitespace($$(this.contextNode).select(xPath, context, { type: XPath.XPathResult.STRING_TYPE })) + rightSide;
  } catch (exception) {
    value = leftSide + '[[[' + xPath + ']]]' + rightSide;
  }
} else {

How to use xpath.XString:

161
162
163
164
165
166
167
168
169
170
    formattedNumber = (characteristicSide + ((mantissaSide) ? decimalFormat.decimalSeparator + mantissaSide : '')).replace(/#/g, '');
  } catch (exception) {
    return decimalFormat.NaN;
  }

  return new XPath.XString(formattedNumber);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*

How to use xpath.select:

74
75
76
77
78
79
80
81
82
83
84
85
    .replace(/^Object.<anonymous>\s*\(/gm, "{anonymous}()@")
    .split("\n");
}


function selectAttributeValue(item, path) {
  var attr = xpath.select(path, item);
  return (attr[0] && attr[0].value) || "";
}


function selectTagValue(item, path) {