我想转换如下所示的 XML:
<products>
<product>
<Name>Bill</Name>
<ID>1</ID>
<Age>19</Age>
</product>
<product>
<Name>Jim</Name>
<ID>2</ID>
<Age>23</Age>
</product>
<product>
<Name>Kathy</Name>
<ID>3</ID>
<Age>53</Age>
</product>
</products>
采用这种格式:集合中的姓名、ID、年龄
如下所示:
collection = [
'Bill-1-19',
'Jim-2-23',
'Kathy-3-53',
];
我认为最好的方法是使用输出正确的 XSLT 样式表?我真的可以使用一些帮助,我已经尝试了好几个小时了。
这是我到目前为止所拥有的:
<!DOCTYPE html>
<html>
<body>
<script>
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else // for IE 5/6
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","after.xml",false);
xhttp.send();
xml=xhttp.responseXML;
var products = xml.getElementsByTagName("products");
var arr = [];
for (var key in products){
var stringVal = "";
var nodes = products[key].childNodes;
for (var ele in nodes){
if(nodes[ele]){
stringVal = stringVal + nodes[ele];
}
}
arr.push(stringVal);
}
console.log(arr);
</script>
</body>
</html>