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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:26:25+00:00 2026-05-25T12:26:25+00:00

If I have a DOM, is it possible to get the reverse XPath of

  • 0

If I have a DOM, is it possible to get the reverse XPath of an element? For example, if I have :

<start>
  <nodes>
    <node>
      <name>Whatever</name>
    </node>
    <node>
      <name>Whatever 2</name>
    </node>
  </nodes>
</start>

If for example, I have a reference to the node with the name Whatever 2, is it possible to get back /start/nodes/node/name[. = "Whatever 2"]?

  • 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-25T12:26:25+00:00Added an answer on May 25, 2026 at 12:26 pm

    Here’s a very simple approach to walking up the tree using the Java DOM API in the Scala REPL:

    First we import the relevant packages and set up our document builder and source:

    scala> import org.w3c.dom._
    import org.w3c.dom._
    
    scala> import javax.xml.parsers._
    import javax.xml.parsers._
    
    scala> val factory = DocumentBuilderFactory.newInstance()
    factory: javax.xml.parsers.DocumentBuilderFactory = ...
    
    scala> val builder = factory.newDocumentBuilder()
    builder: javax.xml.parsers.DocumentBuilder = ...
    
    scala> val source = new org.xml.sax.InputSource()
    source: org.xml.sax.InputSource = org.xml.sax.InputSource@7ecec7c6
    

    Now to parse the example document:

    scala> val content = """<start>
                 <nodes>
                   <node><name>Whatever</name></node>
                   <node><name>Whatever 2</name></node>
                 </nodes>
               </start>"""
    content: java.lang.String = ...
    
    scala> source.setCharacterStream(new java.io.StringReader(content))
    
    scala> val document = builder.parse(source)
    document: org.w3c.dom.Document = [#document: null]
    

    This is a very simple function that recursively walks up the DOM to the document root:

    scala> def path: Node => String = {
         |   case document: Document => ""
         |   case node => path(node.getParentNode) + "/" + node.getNodeName
         | }
    path: org.w3c.dom.Node => String
    

    And we pick the second <name> node to test:

    scala> val node = document.getElementsByTagName("name").item(1)
    node: org.w3c.dom.Node = [name: null]
    

    We get what we expect:

    scala> path(node)
    res1: String = /start/nodes/node/name
    

    It wouldn’t be hard to tweak the path function to avoid explicit recursion or to gather more information as it walks up the tree—for example indicating position when necessary to avoid ambiguity:

    scala> def path(element: Element) = {
         |   def sameName(f: Node => Node)(n: Node) =
         |     Stream.iterate(n)(f).tail.takeWhile(_ != null).filter(
         |       _.getNodeName == n.getNodeName
         |     ).toList
         |   val preceding = sameName(_.getPreviousSibling) _
         |   val following = sameName(_.getNextSibling) _
         |   "/" + Stream.iterate[Node](element)(_.getParentNode).map {
         |     case _: Document => None
         |     case e: Element => Some { (preceding(e), following(e)) match {
         |       case (Nil, Nil) => e.getTagName
         |       case (els, _)   => e.getTagName + "[" + (els.size + 1) + "]"
         |     }}
         |   }.takeWhile(_.isDefined).map(_.get).reverse.mkString("/")
         | }
    path: (element: org.w3c.dom.Element)java.lang.String
    

    Note that I’ve changed the type slightly to make it clear that this will only give us a valid XPath path for elements. We can test:

    scala> path(node.asInstanceOf[Element])
    res13: java.lang.String = /start/nodes/node[2]/name
    

    This is again what we expect.

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

Sidebar

Related Questions

Is it possible to get the name of a node using minidom? For example
Possible Duplicate: How to get nth jQuery element I have this simple code: <ul>
I have a DOM element in memory that is yet to be injected in
I have a org.w3c.dom.Node object. I would like to see if it has any
I have an HTML element with only visible text inside. This example is a
I have an HTML document. It is possible to get the events associated with
I have tried all the possible ideas to get rid of the problem, I
Possible Duplicate: A good Javascript API reference documentation related to browsers and DOM I've
I have the following DOM structure.. <input id=WorkAssignmentID_3462 type=hidden value=3462 name=WorkAssignmentID/> <input id=WorkItemID_3462 type=hidden
I have a DOM Document created from scratch and I need to serialize it

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.