簡體   English   中英

一些 XML 標簽在 Javascript 中返回 null/undefined

[英]some XML tags return null/undefined in Javascript

我在訪問 TEI 格式 XML 文檔的某些標簽中的信息時遇到了一些問題。 XML 的結構如下:

<!--ah.xml-->
<?xml version="1.0"?> 
<TEI>
 <teiHeader>
  <profileDesc>
   <creation>
    <origDate notBefore="1000" notAfter="1200">11th c.</origDate>
    <location>
     <placeName>Samarqand</placeName>
     <geo>39.6270 66.9750</geo>
    </location>
   </creation>
   <titleStmt>
    <title>ʿAtabatu l-Ḥaqāyiq</title>
    <author>Adīb Aḥmad bin Maḥmūd Yuknakī</author>
   </titleStmt>
  </profileDesc>
 </teiHeader>
 <text i="ah" id="ah">
  <s id="s1" translation="Many praises I give to thee, my god.">
   <tok id="t1" pos="NOUN" translation="My god" nform="ilāhī">ilāhī
    <m id="1" type="root" etymology="ara" msd="my.god" form="ilāhī" />
   </tok>
   <tok id="t2" pos="ADJ" translation="many" nform="öküš">öküš
    <m id="2" type="root" etymology="tur" msd="many" form="öküš" />
   </tok>
  </s>
 </text>
</TEI>

奇怪的是,使用下面的代碼我可以從一些標簽中得到我需要的一切,但不是其他的。 這讓我覺得自定義標簽/屬性有一些特別之處,但我找不到正確處理它們的方法。

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var response = this.responseXML;
        console.log(response.querySelector("text").id); // Yeilds "ah".
        console.log(response.querySelector("author").textContent); // Yeilds "Adīb Aḥmad bin Maḥmūd Yuknakī".
        console.log(response.querySelector("title").textContent); // Yeilds "ʿAtabatu l-Ḥaqāyiq".
        console.log(response.querySelector("placeName").textContent); // Throws error: "TypeError: response.querySelector(...) is null".
        console.log(response.querySelector("origDate").notBefore); // Throws error: "TypeError: response.querySelector(...) is null".
        console.log(response.querySelectorAll("tok").translation); // Throws error: "TypeError: response.querySelector(...) is null".
        console.log(response.querySelectorAll("tok").textContent); // Throws error: "TypeError: response.querySelector(...) is null".
    }
};
xhttp.open("GET", "ah.xml", true);
xhttp.setRequestHeader('Content-Type', 'text/xml');
xhttp.responseType = "document";
xhttp.send();

我在訪問 TEI 格式 XML 文檔的某些標簽中的信息時遇到了一些問題。 XML 的結構如下:

<!--ah.xml-->
<?xml version="1.0"?> 
<TEI>
 <teiHeader>
  <profileDesc>
   <creation>
    <origDate notBefore="1000" notAfter="1200">11th c.</origDate>
    <location>
     <placeName>Samarqand</placeName>
     <geo>39.6270 66.9750</geo>
    </location>
   </creation>
   <titleStmt>
    <title>ʿAtabatu l-Ḥaqāyiq</title>
    <author>Adīb Aḥmad bin Maḥmūd Yuknakī</author>
   </titleStmt>
  </profileDesc>
 </teiHeader>
 <text i="ah" id="ah">
  <s id="s1" translation="Many praises I give to thee, my god.">
   <tok id="t1" pos="NOUN" translation="My god" nform="ilāhī">ilāhī
    <m id="1" type="root" etymology="ara" msd="my.god" form="ilāhī" />
   </tok>
   <tok id="t2" pos="ADJ" translation="many" nform="öküš">öküš
    <m id="2" type="root" etymology="tur" msd="many" form="öküš" />
   </tok>
  </s>
 </text>
</TEI>

奇怪的是,使用下面的代碼我可以從一些標簽中得到我需要的一切,但不是其他的。 這讓我覺得自定義標簽/屬性有一些特別之處,但我找不到正確處理它們的方法。

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var response = this.responseXML;
        console.log(response.querySelector("text").id); // Yeilds "ah".
        console.log(response.querySelector("author").textContent); // Yeilds "Adīb Aḥmad bin Maḥmūd Yuknakī".
        console.log(response.querySelector("title").textContent); // Yeilds "ʿAtabatu l-Ḥaqāyiq".
        console.log(response.querySelector("placeName").textContent); // Throws error: "TypeError: response.querySelector(...) is null".
        console.log(response.querySelector("origDate").notBefore); // Throws error: "TypeError: response.querySelector(...) is null".
        console.log(response.querySelectorAll("tok").translation); // Throws error: "TypeError: response.querySelector(...) is null".
        console.log(response.querySelectorAll("tok").textContent); // Throws error: "TypeError: response.querySelector(...) is null".
    }
};
xhttp.open("GET", "ah.xml", true);
xhttp.setRequestHeader('Content-Type', 'text/xml');
xhttp.responseType = "document";
xhttp.send();

暫無
暫無

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

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