XPath utility functions.
Example
var dictXML = <parsed XML document>;// Parsing omitted in this example.
var xpath = dom.XPath.new(dictXML, {
"tei": "http://www.tei-c.org/ns/1.0"
});
var iter = xpath('tei:TEI/tei:text/tei:body/tei:entry').iter(dictXML);
var formXPath = xpath('tei:form');
var orthXPath = xpath('tei:orth');
var pronXPath = xpath('tei:pron');
var defXPath = xpath('tei:sense/tei:cit/tei:quote|tei:sense/tei:def');
dom.iterateAsync(iter, function forEach(entryElement) {
var iter = formXPath.iter(entryElement);
while ((element = iter.iterateNext())) {
console.log(orthXPath.str(element),
pronXPath.str(element), ':');
}
iter = defXPath.iter(entryElement);
while ((element = iter.iterateNext())) {
console.log(element.textContent);
}
}, function batchFinished(batchSize) {
console.log('Just finished a batch of ' + batchSize +
' iterations.');
}, function finished(batchSize) {
console.log('Finished last batch of ' + batchSize +
' iterations.');
});
Methods
(inner) new(document, ns) → {function}
Constructor for an expression compiler function compile(path) specialized for the given document.
When using namespaces, please note that in the XPath API there is no way of specifying the default namespace: if the document has namespaces, even if it does not use prefixes, you must define and use a prefix in your XPath expressions.
Examples
// Extract the title from a TEI document.
var xpath = dom.XPath.new(document, {
"svg": "http://www.w3.org/2000/svg",
"m": "http://www.w3.org/1998/Math/MathML",
"db": "http://docbook.org/ns/docbook",
"tei": "http://www.tei-c.org/ns/1.0"
});
var title = xpath('tei:TEI/tei:teiHeader//tei:title[1]').str(document);
// To avoid re-compiling the expressions each time
// you can either store them in variables or,
// if you prefer to leave the paths at the place where they are used,
// wrap the compiler function into a caching function like this:
var xpath = (function () {
var cache = {};
var xpath = dom.XPath.new(document, {
"svg": "http://www.w3.org/2000/svg",
"m": "http://www.w3.org/1998/Math/MathML",
"db": "http://docbook.org/ns/docbook",
"tei": "http://www.tei-c.org/ns/1.0"
});
return function compileAndCache(path) {
return cache[path] || (cache[path] = xpath(path));
};
})();
var title = xpath('tei:TEI/tei:teiHeader//tei:title[1]').str(document);
Parameters:
Name | Type | Description |
---|---|---|
document |
Document | context document: xpath expressions can only be used inside that document. |
ns |
Object | namespace map: { prefix: url, ... } |
Returns:
- an expression compiler function.
- Type
- function
(inner) compile() → {XPathExpression}
Compile the given XPath string.
This function is the value returned from the
XPath.new() constructor.
The compiled expression has these additional methods apart from the standard XPathExpression.evaluate(contextNode, type, result):
- XPathExpression.node(contextNode)
- XPathExpression.iter(contextNode)
- XPathExpression.bool(contextNode)
- XPathExpression.num(contextNode)
- XPathExpression.str(contextNode)
Returns:
- Type
- XPathExpression
(inner) node(contextNode) → {Node}
Find the first node matched by the given XPath string.
Corresponds to the FIRST ORDERED NODE TYPE
in XPath.
Parameters:
Name | Type | Description |
---|---|---|
contextNode |
Node | starting point for the path. |
Returns:
- XPathResult.singleNodeValue
https://developer.mozilla.org/en-US/docs/Web/API/XPathResult
- Type
- Node
(inner) iter(contextNode) → {XPathResult}
Find all nodes matched by this XPath expression compiled with
compile(path).
Corresponds to the UNORDERED NODE ITERATOR TYPE
in XPath.
This is exactly the same as calling
.evaluate(contextNode, UNORDERED_NODE_ITERATOR_TYPE, null)
.
Other types of iterators are
ORDERED_NODE_ITERATOR_TYPE
,
UNORDERED_NODE_SNAPSHOT_TYPE
,
ORDERED_NODE_SNAPSHOT_TYPE
.
Parameters:
Name | Type | Description |
---|---|---|
contextNode |
Node | starting point for the path. |
Returns:
- iterator with
.iterateNext()
https://developer.mozilla.org/en-US/docs/Web/API/XPathResult → Node
- Type
- XPathResult
(inner) array(contextNode) → {Array}
Build an array with all items from the iterator.
Parameters:
Name | Type | Description |
---|---|---|
contextNode |
Node | starting point for the path. |
Returns:
- array with the results from the iterator.
- Type
- Array
(inner) bool(contextNode) → {boolean}
Check whether any node matches this XPath expression compiled with
compile(path).
Corresponds to the BOOLEAN TYPE
in XPath.
Parameters:
Name | Type | Description |
---|---|---|
contextNode |
Node | starting point for the path. |
Returns:
- XPathResult.booleanValue
https://developer.mozilla.org/en-US/docs/Web/API/XPathResult
- Type
- boolean
(inner) num(contextNode) → {Number}
Get the number value of the first node matched by this XPath expression compiled with
compile(path).
Corresponds to the NUMBER TYPE
in XPath.
Parameters:
Name | Type | Description |
---|---|---|
contextNode |
Node | starting point for the path. |
Returns:
- XPathResult.numberValue
https://developer.mozilla.org/en-US/docs/Web/API/XPathResult
- Type
- Number
(inner) str(contextNode) → {String}
Get the string value of the first node matched by this XPath expression compiled with
compile(path).
Corresponds to the STRING TYPE
in XPath.
Parameters:
Name | Type | Description |
---|---|---|
contextNode |
Node | starting point for the path. |
Returns:
- XPathResult.stringValue
https://developer.mozilla.org/en-US/docs/Web/API/XPathResult
- Type
- String