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 7574653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:29:16+00:00 2026-05-30T16:29:16+00:00

I am wondering how to convert an XPath to a Node object? The reason

  • 0

I am wondering how to convert an XPath to a Node object?

The reason why I ask is because I am trying to create a Range object, and set the range with the XPath. Below is the code I have written, but from my understanding it will not work becasue setRange() and setEnd() needs a Node object as its first parameter.

var range = document.createRange();
range.setStart(startXPath, startOffset);
range.setEnd(endXPath, endOffset);

EDIT: This is how I’m getting my XPath:

function grabSelection() {
    var selection = window.getSelection();
    var range = selection.getRangeAt(0);

    var selectObj = {
        'startXPath': makeXPath(range.startContainer), 
        'startOffset': range.startOffset, 
        'endXPath': makeXPath(range.endContainer), 
        'endOffset': range.endOffset 
   }

   return selectObj
}


function makeXPath (node, currentPath) {
  currentPath = currentPath || '';
  switch (node.nodeType) {
    case 3:
    case 4:
      return makeXPath(node.parentNode, 'text()[' + (document.evaluate('preceding-sibling::text()', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']');
    case 1:
      return makeXPath(node.parentNode, node.nodeName + '[' + (document.evaluate('preceding-sibling::' + node.nodeName, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']' + (currentPath ? '/' + currentPath : ''));
    case 9:
      return '/' + currentPath;
    default:
      return '';
  }
}
  • 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-30T16:29:18+00:00Added an answer on May 30, 2026 at 4:29 pm

    Assuming the thing you called “XPath” is the result of a XPath-query, this returns a DOMNodelist, so you must set

    startXPath to XPathResult[0] 
    

    and

    endXPath to XPathResult[XPathResult.length-1]
    

    (where XPathResult is the nodelist returned by XPath->query)


    Edit related to the comment

    As startXPath and endXPath are really XPath’es, you need to evaluate them to get the nodes:

      var startXPath = document.evaluate(startXPath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
      var endXPath = document.evaluate(endXPath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
    

    Can you explain what you try to achieve, maybe there is an better approach?

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

Sidebar

Related Questions

I was wondering how can I convert the code below into a PHP function
I'm wondering if there's a way to have tabs convert to spaces in a
I was wondering what your opinion about this would be. I'm trying to convert
I have this (XLinq) query and was wondering how to convert it to the
I'm wondering how to convert the following code to output those lines into a
Hey, just wondering how to convert an HH:MM string into a javascript Date object.
I was wondering how to convert a file I have on my server (pdf/excel/ppt)
I was wondering why do people have to convert numbers to string. What are
Hey, I'm just wondering how to convert a numerical date into text format in
I was wondering if it is possible to convert a wpf application to silverlight

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.