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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:54:52+00:00 2026-05-22T11:54:52+00:00

I would like to have xpath, that obtains nodes, that don’t have ancestor, which

  • 0

I would like to have xpath, that obtains nodes, that don’t have ancestor, which is first descendant of specific node.

Let’s assume we have xml document like this:

<a>
  <b>This node</b>
  <c>
    <a>
      <b>not this</b>
      <g>
        <b>not this</b>
      </g>
    </a>
    <a>
      <b>This node</b>
      <c/>
    </a>
  </c>
</a>


<a>
  <c>
    <a>
      <b>not this</b>
    </a>
    <a>
      <b>This node</b> 
    </a>
    <a>
      <b>This node</b> 
    </a>
    <a>
      <b>This node</b> 
    </a>
  </c>
</a>


<d>
  <b>This node</b>
</d>

I would like to select all b nodes in document that don’t have as their ancestor node //a/c/a[1].

  • 1 1 Answer
  • 2 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-22T11:54:52+00:00Added an answer on May 22, 2026 at 11:54 am

    I would like to select all b nodes in
    document that don’t have as their
    ancestor node //a/c/a[1]

    Use this XPath expression:

    //b[not(ancestor::a
                 [parent::c[parent::a]
                and
                  not(preceding-sibling::a)
                 ]
           )
       ]
    

    This selects all b elements in the document that don’t have ancestor a that has a parent c that has parent a and the a ancestor that has parent c is not the first a child of its parent.

    Given the following XML document (based on the provided, but made well-formed and also put identifying text in the nodes that should be selected):

    <t>
        <a>
            <b>This node 1</b>
            <c>
                <a>
                    <b>not this</b>
                    <g>
                        <b>not this</b>
                    </g>
                </a>
                <a>
                    <b>This node 2</b>
                    <c/>
                </a>
            </c>
        </a>
        <a>
            <c>
                <a>
                    <b>not this</b>
                </a>
                <a>
                    <b>This node 3</b>
                </a>
                <a>
                    <b>This node 4</b>
                </a>
                <a>
                    <b>This node 5</b>
                </a>
            </c>
        </a>
        <d>
            <b>This node 6</b>
        </d>
    </t>
    

    exactly the wanted 6 b elements are selected.

    Verification using XSLT:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
     <xsl:copy-of select=
     "//b[not(ancestor::a
                 [parent::c[parent::a]
                and
                  not(preceding-sibling::a)
                 ]
             )
         ]
    
     "/>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the above XML document, exactly the wanted b elements are selected and copied to the output. The wanted, correct result is produced:

    <b>This node 1</b>
    <b>This node 2</b>
    <b>This node 3</b>
    <b>This node 4</b>
    <b>This node 5</b>
    <b>This node 6</b>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I would like have solved via a SQL query.
So in my project i would like have a nice treeview that has images.
I would like to have it so that when I play one Vimeo video
I would like to have an interface that allows for a generic type public
Ive got a large XML set, which I would like to run some xpath
I would like to write a greasemonkey script that given an xpath returns all
I have some HTML that looks like this: <h1 id=header>Header</h1> I would like to
I have a shoulda macro/method that tests controller responses for XPath conditions like so:
I would like to have Java generate a '0' in case an XPath expression
I have a list of name/value pairs that I would like to map into

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.