To retrieve a list of nodes using XPath in JavaScript, you can try using the document.evaluate() method. This updated code snippet will select all cac:ContractDocumentReference nodes with cbc:ID equal to '234' from the provided XML. Adjust the XPath expression and the node parsing logic based on your specific requirements.
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
const select = xpath => {
const resolver = xmlDoc.createNSResolver(xmlDoc.documentElement);
const result = xmlDoc.evaluate(
xpath,
xmlDoc,
resolver,
XPathResult.ANY_TYPE,
null
);
const nodes = [];
let node = result.iterateNext();
while (node) {
nodes.push(node);
node = result.iterateNext();
}
return nodes;
};
const nodes = select("//cac:ContractDocumentReference[cbc:ID='234']");
nodes.forEach(node => {
const id = node.querySelector('cbc\\:ID').textContent;
const issueDate = node.querySelector('cbc\\:IssueDate').textContent;
console.log(`ID: ${id}, Issue Date: ${issueDate}`);
});