How to use the useNamespaces function from xpath

Find comprehensive JavaScript xpath.useNamespaces code examples handpicked from public code repositorys.

xpath.useNamespaces is a method in XPath library that defines the namespace mappings for a document.

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"
});

fork icon48
star icon60
watch icon17

11
12
13
14
15
16
17
18
19
20
21
22
23
24


const DOMParser = xmldom.DOMParser;
const ProtocolError = errors.ProtocolError;
const ResponseValidator = responseValidation.ResponseValidator;


const select = xpath.useNamespaces(namespaces);


module.exports = {


	// methods used by rest of app
fork icon6
star icon16
watch icon34

How does xpath.useNamespaces work?

The useNamespaces method of the XPath library in JavaScript sets the namespace prefixes for XPath expressions. It takes an object as an argument with namespace prefixes as keys and their URIs as values. When evaluating an XPath expression, the specified prefixes are used to resolve any namespace-related expressions.

32
33
34
35
36
37
38
39
40
41
}
//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"});
var a = String(select("//text()[preceding-sibling::x:h2[//preceding-sibling::x:div[@class='registroDescripcion']] and following-sibling::x:h2[//preceding-sibling::x:div[@class='registroDescripcion']]]",d).toString());
a = a.split("\t").join("").split("\n");
a = a.filter((input) => {
	return input != "";
fork icon1
star icon0
watch icon0

4
5
6
7
8
9
10
11
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
});
fork icon0
star icon2
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const xpath = require("xpath");
const dom = require("xmldom").DOMParser;

const xml = `

Everyday Italian
Giada De Laurentiis
2005
30.00

Harry Potter
J K. Rowling
2005
29.99

`;

// Parse the XML document
const doc = new dom().parseFromString(xml);

// Define the namespaces to use
const namespaces = {
  books: "http://example.com/books",
};

// Use the namespaces in an XPath query
const nodes = xpath.select("//books:book", doc, true, namespaces);

console.log(nodes.length); // Output: 2

In this example, xpath.useNamespaces is used to define the namespace prefixes used in an XPath query. The xpath.select method then uses these namespace prefixes to select all books:book elements in the XML document.

75
76
77
78
79
80
81
82
83
84

if(handlerConfiguration.matcher){

	var doc = new dom().parseFromString(req_body);
	//console.log("UsingNamespaces;", handlerConfiguration.namespaces);
	var select = xpath.useNamespaces(handlerConfiguration.namespaces);

	// Find the first matcher
	_.find(handlerConfiguration.matcher, function(matcher){
			var match = select(matcher.xpath, doc).toString();
fork icon0
star icon1
watch icon1

43
44
45
46
47
48
49
50
51
52
  };
  parent = parent.parentNode;
}

// it's necessary to set the available namespaces, selecting a namespaced xpath will fail otherwise
const xpathSelect = xpath.useNamespaces(namespaces);
// xml is text-based which means everything is a string: explicitly select a string value
const selection = xpathSelect(`string(${selector})`, item, true);
// run custom parser
value = parser(selection);
fork icon0
star icon0
watch icon1

+ 70 other calls in file