Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to get the value of parent node attribute in Javascript xpath
    text
    copied!<p>I have the code below working well. It searches my xml file for all the "outcome_text" nodes of a particular branch (the 'str' branch), then writes the text to a table. Problem is, I would really like to get the value of the parent nodes attribute as well (the overall id value). That way my table row would be two columns: OVerall ID and Outcome. I can do it no problem in VB/asp xpath, but trying to make a client side version for mobile. here is a snippet of the xml:</p> <pre><code> &lt;curriculum&gt; &lt;strand id="Dance"&gt; &lt;strand_text&gt;Dance&lt;/strand_text&gt; &lt;overalls&gt; &lt;overall id="A1"&gt; &lt;overall_text&gt;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&lt;/overall_text&gt; &lt;specifics&gt; &lt;specific&gt;&lt;/specific&gt; &lt;/specifics&gt; &lt;/overall&gt; &lt;overall id="A2"&gt; &lt;overall_text&gt;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&lt;/overall_text&gt; &lt;specifics&gt; &lt;specific&gt;&lt;/specific&gt; &lt;/specifics&gt; &lt;/overall&gt; &lt;/overalls&gt; &lt;/strand&gt; &lt;strand id="visual"&gt; . . . &lt;/strand&gt; &lt;/curriculum&gt; </code></pre> <p>and here is the code: (ignore the IE bits--it will be on Android and IOS only)</p> <pre><code> &lt;script&gt; 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 &lt; 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 &amp;&amp; document.implementation.createDocument) { var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null); var result = nodes.iterateNext(); var txt = "&lt;table border='1'&gt;&lt;tr&gt;&lt;th&gt;Strand&lt;/th&gt;&lt;th&gt;OVERALL&lt;/th&gt;&lt;/tr&gt;"; while (result) { ind = str; // but would like the value of outcome node attribute var over = result.childNodes[0].nodeValue txt = txt + "&lt;tr&gt;&lt;td&gt;" + ind + "&lt;/td&gt;&lt;td&gt;" + over + "&lt;/td&gt;&lt;/tr&gt;" result = nodes.iterateNext(); } } txt = txt + "&lt;/table&gt;" document.getElementById('outtable').innerHTML = txt; } </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload