Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:11:27+00:00 2026-05-16T05:11:27+00:00

I’m loading an xml document using JavaScript (Jquery $.ajax ). I need to be

  • 0

I’m loading an xml document using JavaScript (Jquery $.ajax).

I need to be able to count the number of branches (b) separating 2 text nodes (s).

E.g.

    <b n="Archaea">
        <s>Archaea</s>
        <b n="Eubacteria">
            <s>Cyanobacteria</s>
            <s>Spirochaete</s>
            <b n="Seaweeds">
                <s>Red rags</s>
                <s>Calliblepharis</s>
            </b>
        </b>
        <b n="Land plants">
            <s>Liverwort</s>
            <s>Moss</s>
            <s>Bracken fern</s>
            <b n="Seed plants">
                <s>Scots pine</s>
                <s>Ginko</s>
                <s>Welwitschia</s>
            </b>
        </b>
    </b>

So, how many branches is ‘Scots pine’ away from ‘Calliblepharis’, for example. In this case, the answer would be 4 (Seed plants > Land plants > Archaea > Eubacteria > Seaweeds).

I also need to calculate the ‘closest common ancestor’ between 2 elements. For example, between ‘Scots pine’ and ‘Ginko’ it would be ‘Bracken fern’ (because Bracken fern is the closest species to the branch that contains ‘Scots pine’ and ‘Ginko’). I’m really not sure how this would work when the 2 elements are very far from each other in different branches.

Sorry if I’m using the wrong language here. Hope it makes sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:11:28+00:00Added an answer on May 16, 2026 at 5:11 am

    Sorry about the late reply.

    I’ve set up a a demo at jsbin

    Hopefully it’s fairly self explanatory but if not ask me.

    For the xhr bit you need to have an file.xml in the same directory as the page.

    This is the main function that gets the distance between the branches

        function elementDistance(elem1, elem2) {
            var steps = 0;
            //the parent elements are the branches
            var first = elem1.parentElement;
            var second = elem2.parentElement;
            //if the elements are at different depths you need to even them up
            //and count each time as a step
            if (elem1.depth() > elem2.depth()) {
                while (first.depth() > second.depth()) {
                    first = first.parentElement;
                    steps++;
                }
            }
            else if (elem1.depth() < elem2.depth()) {
                while (first.depth() < second.depth()) {
                    second = second.parentElement;
                    steps++;
                }
            }
            while (first !== second) {
                steps += 2;
                first = first.parentElement;
                second = second.parentElement;
            }
            return steps;
        }
    

    p.s. the demo doesn’t work in firefox or IE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I used javascript for loading a picture on my website depending on which small
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.