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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:23:35+00:00 2026-06-06T18:23:35+00:00

How would I write an xpath query to match the <subitem> element in the

  • 0

How would I write an xpath query to match the <subitem> element in the XML snippet below?

Specifically I want to match any element that has a “name” attribute that matches the value of the “name” attribute of the root. There may be an arbitrary number of ancestors between <root> and <subitem>.

<root name="xyz">
  <anything>
    <subitem name="xyz" />
  </anything>
</root>

My goal is to write a PMD rule for finding cases of Logger.getLogger() that use a different class than the class the statement is in.

  • 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-06-06T18:23:39+00:00Added an answer on June 6, 2026 at 6:23 pm

    Use this XPath expression:

    //*[@name = /*/@name]
    

    This selects any element (including the top element named root), whose name attribute has the same string value as that of the name attribute of the top element. When this XPath expression is evaluated against the provided XML document, two elements — root and subitem are selected.

    I think that only the second element of these two is wanted. For this, use the following XPath expression:

    //*/*[@name = /*/@name]
    

    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:template match="/">
       <xsl:copy-of select="//*/*[@name = /*/@name]"/>
    ========
       <xsl:copy-of select="//*[@name = /*/@name]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <root name="xyz">
        <anything>
            <subitem name="xyz" />
        </anything>
    </root>
    

    the two XPath expressions are evaluated and the selected nodes are copied to the output:

    <subitem name="xyz"/>
    ========
       <root name="xyz">
        <anything>
          <subitem name="xyz"/>
        </anything>
       </root>
       <subitem name="xyz"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know any library that will help doing that? I would write a
How would I write a SQL query that excludes a record if one (external)
I would like to write a greasemonkey script that given an xpath returns all
Given a piece of Xml Like the one below. How would I write an
How would you write a SQL query to get the size of blob in
How would one write a sql server DDL script that can: For each table
How would I write a SQLite query to select all records from a specific
I want to write a function that identifies all the links on a particular
Using XPath, I would like to Match whole word (option for user, just like
Is it possible to write an XPath expression that gets the root node of

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.