0

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:

 <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>

and here is the code: (ignore the IE bits--it will be on Android and IOS only)

 <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;
}

Cory
  • 1,263
  • 16
  • 31
  • Note that IE10+ does not have native XPath support. http://stackoverflow.com/questions/13521554/xpath-in-internet-explorer-10-gone. You might want to streamline your entire approach using the library mentioned in that post. – Tomalak Dec 10 '13 at 13:40

1 Answers1

1

Seems to me something like:

ind = result.parentNode.getAttribute("id")

should work.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • OMG, of course that worked! (just like the vb code). That was the first thing I tried, but I had "result.parentNode[0].getAttribute("id") and .value on the end. I tried all kinds of variations (.nodesValue etc.). For some reason I thought I needed [0] for all in js xpath.I shall now go and put my head in the sand. – Cory Dec 11 '13 at 02:08