我的XML看起来像这样:
<ns2:OrderList>
<order order_id="123" item_name="123"/>
<order order_id="234" item_name="1233"/>
<order order_id="2357" item_name="1234"/>
......
</ns2:OrderList>
我想使用XPath.js from npjms 如何使用&#34; order_id&#34;价值观????
答案 0 :(得分:0)
var select = require('xpath.js')
, dom = require('xmldom').DOMParser
var xml = `<ns2:OrderList>
<order order_id="123" item_name="123"/>
<order order_id="234" item_name="1233"/>
<order order_id="2357" item_name="1234"/>
</ns2:OrderList>`;
var doc = new dom().parseFromString(xml);
var nodes = select(doc, "//order/@order_id");
var orderIds = [];
nodes.forEach(function(node) {
orderIds.push(node.nodeValue);
});
console.log(orderIds);
答案 1 :(得分:0)
如果你想使用xpath.js(你还需要xmldom进行解析),这样的东西应该可以解决问题。
const Dom = require('xmldom').DOMParser;
const select = require('xpath.js');
const xml = '<ns2:OrderList> <order order_id="123" item_name="123"/> <order order_id="234" item_name="1233"/> <order order_id="2357" item_name="1234"/> </ns2:OrderList>¬';
const doc = new Dom().parseFromString(xml);
const nodes = select(doc, '//order');
const orderIds = nodes.map((node) => node.getAttribute('order_id'));
console.log(orderIds);