javascript - Jquery: Count node separation in xml -
I am loading an XML document using Javascript (Jquery $. Ajax
).
I should be able to calculate the number of branches (B) Separating 2 text nodes.
Example
& lt; Bn = "ARCHIA" & gt; & Lt; S & gt; Archia & lt; / S & gt; & Lt; B n = "Ubetaria" & gt; & Lt; S & gt; Cyanobacteria & lt; / S & gt; & Lt; S & gt; Spirochaete & lt; / S & gt; & Lt; B n = "sewds" & gt; & Lt; S & gt; Red Rugs & lt; / S & gt; & Lt; S & gt; Calliblepharis & lt; / S & gt; & Lt; / P & gt; & Lt; / P & gt; & Lt; B n = "ground plants" & gt; & Lt; S & gt; Liverwort & lt; / S & gt; & Lt; S & gt; Mass & lt; / S & gt; & Lt; S & gt; Bracken Fern & lt; / S & gt; & Lt; B n = "seed plants" & gt; & Lt; S & gt; Scots Pine & lt; / S & gt; & Lt; S & gt; Ginkgo & lt; / S & gt; & Lt; S & gt; Welwitschia & lt; / S & gt; & Lt; / P & gt; & Lt; / P & gt; & Lt; / P & gt;
So, how many branches are 'off the Scots pine' from 'CallBarfare', for example. In this case, the answer will be 4 (seed plants> land plants> arcusa> ebacteria> sewds).
For the 'closest common ancestor' among the two elements, for example, this would be a 'bracken fern' between 'Scots Pine' and 'Ginkgo' (because Brackson is the closest species of ferns branches in which Scotts Pine And Ginku). I'm not sure how this will work when 2 elements are far apart from each other in different branches.
Sorry, if I'm using the wrong language here. Hope this makes sense.
Sorry about late reply.
I
Hope this is quite self explanatory, but if I do not ask.
For the xhr bit you should have a file.xml
same directory as the page.
This is the main task that achieves the distance between the branches
function element distance (elem1, elem2) {var steps = 0; // Parental element branches are the first = elem1.parentElement; Var seconds = elem2.parentElement; // If the elements are at different depths, then you also need to do them up / and count each time as a step if (elem1.depth ()> elem2.depth ()) {while (first .depth ()> Second .depth ()) {first = first.parentElement; Stages ++; }} And if (elem1.depth ()) & lt; Elem2.depth ()) {while (first.depth ()) & lt; Second.depth ()) {second = second.parentElement; Stages ++; }} While (first! == seconds) {steps + = 2; First = first point element; Second = Second. } Step back; }
PS The demo does not work in Firefox or IE
Comments
Post a Comment