我有一个XML文件(已精简):
<?xml version="1.0" encoding="UTF-8"?>
<Labels Version="24100002">
<Attendant>Attendant</Attendant>
<Attendant Index="1">Car Park Attendant</Attendant>
<Attendant Index="2">Entrance Attendant</Attendant>
<Attendant Index="3">Auditorium Attendant</Attendant>
<Attendant Index="4">Attendant 4</Attendant>
</Labels>
而且,我有一个用于加载文件的JavaScript:
function loadXMLDoc(filename) {
return fetch(filename)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
在我的HTML文件中,我这样做(精简):
// Code snipped
// Fetch the XML and XSL files asynchronously
var [xsl, xml, labelsInfo] = await Promise.all([
loadXMLDoc('DutyHistory-transform.xsl'),
loadXMLDoc('DutyAssignHistory.xml'),
loadXMLDoc('LabelsInfo.XML')
]);
// Code snipped
// We have to add 1 to the indexing because the LabelsInfo file has a "Attendant" label too!
xsltProcessor.setParameter(null, 'index13', labelsInfo.documentElement.getElementsByTagName('Attendant')[1].textContent.trim());
xsltProcessor.setParameter(null, 'index14', labelsInfo.documentElement.getElementsByTagName('Attendant')[2].textContent.trim());
xsltProcessor.setParameter(null, 'index15', labelsInfo.documentElement.getElementsByTagName('Attendant')[3].textContent.trim());
有什么办法可以直接得到所需的吗
Attendant
@Index