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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:25:48+00:00 2026-06-10T18:25:48+00:00

I have an XML fragment like <main> <sub1> <element> <sub2> <element> <sub3/> </element> </sub2>

  • 0

I have an XML fragment like

<main>
    <sub1>
        <element>
            <sub2>
                <element>
                    <sub3/>
                </element>
            </sub2>
        </element>
    </sub1>
    <sub4>
        <sub5>
            <element>
                <sub6>
                    <element>
                        <sub7/>
                    </element>
                </sub6>
            </element>
        </sub5>
    </sub4>
    <sub8>
        <element/>
    </sub8>
</main>

If main is the current node, I can select all element nodes like this:

select=".//element"

This gives me 5 matches in this example. But how do I select only the first element node in the hierarchy, so the ones after sub1, sub5 and sub8? I need to ignore all elements that have another element between them and the original main node.

The number of sub nodes in between may be more than 1 or 2 as in the above example, and the main node in the example may be part of another element node above.

thanks
K

  • 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-10T18:25:50+00:00Added an answer on June 10, 2026 at 6:25 pm

    Use:

    .//element[(ancestor::main|ancestor::element)[last()][self::main]]
    

    Here is a simple verification with 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:for-each select=
       ".//element
           [(ancestor::main|ancestor::element)
                              [last()][self::main]
           ]">
       <xsl:copy-of select="."/>
    ==================
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <main>
        <sub1>
            <element>
                <sub2>
                    <element>
                        <sub3/>
                    </element>
                </sub2>
            </element>
        </sub1>
        <sub4>
            <sub5>
                <element>
                    <sub6>
                        <element>
                            <sub7/>
                        </element>
                    </sub6>
                </element>
            </sub5>
        </sub4>
        <sub8>
            <element/>
        </sub8>
    </main>
    

    the Xpath expression is evaluated with the current node being the top node in the document, then the results of this evaluation are copied to the output, using a convenient to notice delimiter string:

    <element>
    
       <sub2>
    
          <element>
    
             <sub3/>
    
          </element>
    
       </sub2>
    
    </element>
    ==================
      <element>
    
       <sub6>
    
          <element>
    
             <sub7/>
    
          </element>
    
       </sub6>
    
    </element>
    ==================
      <element/>
    ==================
    

    Explanation:

    The expression:

        .//element
           [(ancestor::main|ancestor::element)
                                         [last()]
                                            [self::main]
           ]
    

    selects all descendat element elements of the current node the first of the two possible ancestors (upwards) main or element is main.

    That is, there isn’t an element that is an ancestor of the selected element and happens (on the way upwards) before the main ancestor.

    To understand why last() is used as a predicate and not just [1], we need to remember that the nearest of two ancestors is the last of them in document order.

    Do note:

    This XPath expression correctly selects all wanted element elements, even in the case when the current node has itself an element ancestor.

    An expression like the one proposed in the answer by Ian Roberts:

    .//element[not(ancestor::element)] 
    

    selects nothing in this case.


    Update:

    As noted by Michael Kay, if we further generalize this problem and allow other main elements to appear anywhere in the document, then the proposed expression in this answer may select “false-positives”.

    Here is an updated expression (no longer a pure/standalone XPath expression, as it uses the XSLT function current()) that still produces the correct count in this situation:

    .//element
       [(ancestor::main|ancestor::element)
                          [last()][self::main]
      and
        count(current()|ancestor::main[1]) = 1
       ]"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a schema fragment that looks like <xs:element name=dataValue> <xs:complexType> <xs:sequence> <xs:element name=value
The problem is this: I have an XML fragment like so: <fragment>text1 <a>inner1 </a>text2
I have a xml fragment like below <Detail uid=6> <![CDATA[ <div class=heading>welcome to my
I have an xml layout for a Fragment (android.support.v4.app.Fragment). When this Fragment is added
I have xml like this: <rule> <word>I</word> <word>need</word> <word>more</word> <marker> <word>money</word> </marker> <word>now</word> </rule>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
I have XML like <A> <B> <C> Hello World </C> </B> </A> I want
I have a noob LINQ to XML question. I have xml like so: <pages>
I have an input XML document something like this: <text> <p> Download the software

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.