代码之家  ›  专栏  ›  技术社区  ›  Andrew Truckle

如何使用JavaScript获取具有特定属性值的XML节点的值?

  •  0
  • Andrew Truckle  · 技术社区  · 2 年前

    我有一个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

    1 回复  |  直到 2 年前
        1
  •  1
  •   mplungjan    2 年前

    const str = `<?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>`
    
    const doc = new window.DOMParser().parseFromString(str, "text/xml");
    let index = 13;
    doc.querySelectorAll('Attendant[Index]')
      .forEach((att,i) => {
        if (i<3) {
          console.log(`index${index+i}`,att.textContent);
          // xsltProcessor.setParameter(null, `index${index+i}`, att.textContent);  
        }  
    });
        2
  •  0
  •   Andrew Truckle    2 年前

    xsltProcessor.setParameter(null, 'index13', labelsInfo.documentElement.querySelectorAll("Attendant[Index='1']")[0].textContent);
    xsltProcessor.setParameter(null, 'index14', labelsInfo.documentElement.querySelectorAll("Attendant[Index='2']")[0].textContent);
    xsltProcessor.setParameter(null, 'index15', labelsInfo.documentElement.querySelectorAll("Attendant[Index='3']")[0].textContent);