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

  • Home
  • SEARCH
  • 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 7510221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:09:50+00:00 2026-05-29T23:09:50+00:00

I am trying to find all the element names which follow the below two

  • 0

I am trying to find all the element names which follow the below two rules.

1. elements should have the <set>erase</set>

2. if two or more elements have the <set>erase</set> in hierarchy (Ex: <b> and <d> both have <set>erase</set>) then only the parent node name has to be printed(ie <b> in this case).

So the required result for below xml needs to be :

b , y , p

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
    <b>
        <set>erase</set>
        <d>
        <set>erase</set>
        </d>
    </b>

    <c>
        <x>
        </x>
    </c>

    <e>

        <y>
                    <set>erase</set>
            <q>
            </q>
        </y>
        <z>
            <p>
            <set>erase</set>
            </p>
        </z>
    </e>
</a>

When I use the query = (//set[contains(.,'erase')])[1] I get only node b in result set.
When I use the query = //set[contains(.,'erase')] I get all nodesList b,d,y,p in result set.

Can anyone help me find the query to result in nodeList b , y and p.

Here is the java code snippet I used.

        XPath xpath = factory.newXPath();
    String query = "//set[contains(.,'erase')]";
            XPathExpression expr=null;
    try {
        expr = xpath.compile(query);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        Object result = null;
    try {
        result = expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    NodeList nodes = (NodeList) result;


    for (int i = 0; i < nodes.getLength(); i++) {
        String x = "";
        Node n = nodes.item(i).getParentNode();
        x=n.getNodeName();
        while(!n.getNodeName().equalsIgnoreCase(request.getClass().getSimpleName())){
            if ((n = n.getParentNode())!=null){
                x=n.getNodeName()+"."+x;
            }
        }



        System.out.println("Path: "+x);

output:

a.b
a.b.d
a.e.y
a.e.z.p

Could anyone help me figure out the query which results in only a.b , a.e.y and a.e.z.p
Let me know if you need more details. or any other use-case.

  • 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-29T23:09:54+00:00Added an answer on May 29, 2026 at 11:09 pm

    One expression that selects exactly the wanted elements is:

          //*[set[. = 'erase' and not(node()[2])]
             and
              not(ancestor::*
                     [set
                        [. = 'erase' and not(node()[2])]
                     ]
                  )
              ]
    

    XSLT – based verification:

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
         <xsl:strip-space elements="*"/>
    
         <xsl:template match="/">
             <xsl:for-each select=
             "//*[set[. = 'erase' and not(node()[2])]
                 and
                  not(ancestor::*
                         [set
                            [. = 'erase' and not(node()[2])]
                         ]
                      )
                  ]">
    
              <xsl:value-of select="name()"/>
              <xsl:text>&#xA;</xsl:text>
            </xsl:for-each>
         </xsl:template>
    </xsl:stylesheet>
    

    This transformation, when applied on the provided by Sean B. Durkin XML document:

    <a>
        <b>
            <set>erase</set>
            <set>
                <a/>erase
            </set>
            <d>
                <set>erase</set>
            </d>
        </b>
        <c>
            <x>         </x>
        </c>
        <e>
            <y>
                <set>erase</set>
                <q>             </q>
            </y>
            <z>
                <p>
                    <set>erase</set>
                </p>
            </z>
        </e>
    </a>
    

    evaluates the XPath expression above and outputs the names of the selected elements — the wanted, correct result is produced:

    b
    y
    p
    

    Do note that the following two expressions are quite incorrect:

    *[set[text()='erase']][not(ancestor::*[set[text()='erase']])]  
    

    Or:

    *[set[text()='erase']][ancestor::*[set[text()!='erase']]] 
    

    These two expressions suffer from more than one problem:

    1. They are relative expressions and regardless with which initial context they are applied, they cannot select all wanted elements in an hierarchy with undefined depth and structure.

    2. set[text()='erase'] selects not only an element of the form:

    …

    <set>erase</set>
    

    but also elements of the form:

    <set>
    xyz
     <a/>erase</set>   
    

    .3. Similarly:

    set[text()!='erase']   
    

    selects elements of the form:

    <set>
    xyz
     <a/>erase</set> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find and collect all the elements that meet these requirements: var
Guys, I'm trying to write xpath or css to find/click on list element All
I am trying to define a JQuery statement for finding elements which have a
This is what i'm trying to do in pseudo code Find all <td> Elements
I am trying to find all ruby files in the project. However I want
I'm trying to find all permutations of a string and sort them alphabetically. This
I'm trying to find all instances of an advert on a website. The advert
Hi I am trying to find all js & css files in one find
I am trying to find all of the types in the Models namespace within
I am trying to find all floating number (could be in exponential forms with

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.