簡體   English   中英

使用 XPath 創建 XML 屬性

[英]Create XML attribute using XPath

我想創建一個 xml 屬性,但為了找到要添加查詢的元素,我需要使用 xpath。 我怎樣才能做到這一點?

示例 =

const xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title id="somethingeng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>`;

var doc = new DOMParser().parseFromString(xmlText,'text/xml');

var r = doc.evaluate("//*[@lang[contains(.,'eng')]]", doc, null, XPathResult.ANY_TYPE, null);

我想為這個 r 創建一個屬性;

首先xmlText字符串現在應該是一個模板文字,它有新的行。

evaluate()接縫很好,但結果是需要使用XPathResult.iterateNext ()迭代的 XPathResult。

 const xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title id="somethingeng">Learning XML</title> <price>39.95</price> </book> </bookstore>`; var doc = new DOMParser().parseFromString(xmlText,'text/xml'); var r = doc.evaluate("//*[@lang[contains(.,'eng')]]", doc, null, XPathResult.ANY_TYPE, null); var next = r.iterateNext(); while (next) { console.log(next.textContent); next = r.iterateNext(); }

更新

根據迭代器,您需要收集您感興趣的節點,然后再更改它們。 在下文中,我創建了可以基於 XPath 表達式和表示新數據的對象創建子元素和屬性的函數。

 const xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title id="somethingeng">Learning XML</title> <price>39.95</price> </book> </bookstore>`; var doc = new DOMParser().parseFromString(xmlText, 'text/xml'); function addAttribute(doc, xpath, obj) { let r = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null); let nodes = []; let next = r.iterateNext(); while (next) { nodes.push(next); next = r.iterateNext(); } nodes.forEach(node => { Object.keys(obj).forEach(key => { let newattr = doc.createAttribute(key); newattr.value = obj[key]; node.setAttributeNode(newattr); }); }); } function addChildNode(doc, xpath, obj) { let r = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null); let nodes = []; let next = r.iterateNext(); while (next) { console.log(next.textContent); nodes.push(next); next = r.iterateNext(); } nodes.forEach(node => { Object.keys(obj).forEach(key => { let newnode = doc.createElement(key); newnode.textContent = obj[key]; node.appendChild(newnode); }); }); } addAttribute(doc, "//title[@lang[contains(.,'eng')]]", {"data-lang":"eng", index: 2}); addChildNode(doc, "//book[number(price) < 30]", {sale: true}); console.log(doc.documentElement.outerHTML);

通過使用XPathResult.snapshotItem()找到了解決方案。

 var xmlText = `<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title id=\"somethingeng\">Learning XML</title> <price>39.95</price> </book> </bookstore>`; var doc = new DOMParser().parseFromString(xmlText, 'text/xml'); var r = doc.evaluate("//*[@id[contains(.,'eng')]]", doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); var index = 0; while (index < r.snapshotLength) { var next = r.snapshotItem(index); next.setAttribute("value", "val"); index++; } var xmlSerializer = new XMLSerializer(); const updatedDoc = xmlSerializer.serializeToString(doc); console.log(updatedDoc);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM