我的代碼如下運行良好。它在我的xml文件中搜索特定分支('str'分支)的所有「outcome_text」節點,然後將文本寫入表中。問題是,我真的很想獲得父節點屬性的值(整體id值)。這樣,我的表格行將是兩列:OVerall ID和結果。 我可以做到這一點在VB/asp xpath中沒有問題,但試圖爲移動設備製作客戶端版本。 這裏是XML的一個片段:是否有可能在Javascript xpath中獲得父節點屬性的值
<curriculum>
<strand id="Dance">
<strand_text>Dance</strand_text>
<overalls>
<overall id="A1">
<overall_text>Creating and Presenting: apply the creative process (see pages 19-22) to the composition of simple dance phrases, using the elements of dance to communicate feelings and ideas</overall_text>
<specifics>
<specific></specific>
</specifics>
</overall>
<overall id="A2">
<overall_text>Reflecting, Responding, and Analysing: apply the critical analysis process (see pages 23-28) to communicate their feelings, ideas, and understandings in response to a variety of dance pieces and experiences</overall_text>
<specifics>
<specific></specific>
</specifics>
</overall>
</overalls>
</strand>
<strand id="visual"> . . . </strand>
</curriculum>
和這裏是代碼:(忽略IE比特 - 這將是在Android和僅IOS)
<script>
function popout() {
var gr = gradedd.options[gradedd.selectedIndex].value
var sb = subdd.options[subdd.selectedIndex].value
var str = stranddd.options[stranddd.selectedIndex].value
xml = loadXMLDoc("resources/ont/grade_" + gr + "_" + sb + ".xml");
path = "/curriculum/strand[@id='" + str + "']/overalls/overall/overall_text"
// code for IE
if (window.ActiveXObject) {
var nodes = xml.selectNodes(path);
for (i = 0; i < nodes.length; i++) {
// ddlist[0] = nodes[i].childNodes[0].nodeValue; ignore
// dropdown[dropdown.length] = new Option(ddlist[i], ddlist[i]); ignore
}
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
var txt = "<table border='1'><tr><th>Strand</th><th>OVERALL</th></tr>";
while (result) {
ind = str; // but would like the value of outcome node attribute
var over = result.childNodes[0].nodeValue
txt = txt + "<tr><td>" + ind + "</td><td>" + over + "</td></tr>"
result = nodes.iterateNext();
}
}
txt = txt + "</table>"
document.getElementById('outtable').innerHTML = txt;
}
請注意,IE10 +沒有本機XPath支持。 http://stackoverflow.com/questions/13521554/xpath-in-internet-explorer-10-gone。您可能想要使用該帖子中提到的庫簡化您的整個方法。 – Tomalak