I am attempting to use the Javascript Document evaluate
function with valid XPath that uses the name()
/ local-name()
function but am consistently getting the following error in Jfiddle:
The string '/foods/*[position()=1</a>/local-name()' is not a valid XPath expression
It is unclear how or why the </a>
fragment is introduced
Sample code is as follows:
const xml = "<foods><meat>chicken</meat><fruit>orange</fruit><fruit>apple</fruit><fruit>banana</fruit></foods>";
const doc = new DOMParser().parseFromString(xml,'application/xml');
for ( var i = 0 ; i < 4; i++ ){
const xpath = `/foods/*[position()=${i+1}]`;
const xpathValue = document.evaluate(
xpath,
doc,
null,
XPathResult.STRING_TYPE,
null
);
const xpathName = document.evaluate(
`${xpath}/local-name()`,
doc,
null,
XPathResult.STRING_TYPE,
null
);
console.log(`${xpathName.stringValue} = ${xpathValue.stringValue}`);
}
The XPath evalutaing the value works fine, just not the node name. Any assistance would be appreciated.
2
Answers
Thanks @Martin Honnen - working code
You can use XPath 3.1 with SaxonJS 2: