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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:41:44+00:00 2026-05-15T05:41:44+00:00

I am new to XPath, and from what I have read in some tutorials

  • 0

I am new to XPath, and from what I have read in some tutorials about axes, I am still left wondering how to implement them. They aren’t quite behaving as I had expected. I am particularly interested in using ancestor and descendant axes.

I have the following XML structure:

<file>
    <criteria>
        <root>ROOT</root>
        <criterion>AAA</criterion>
        <criterion>BBB</criterion>
        <criterion>CCC</criterion> 
    </criteria>
    <format>
        <sort>BBB</sort>
    </format>
</file>

And I have the following XSL:

<xsl:template match="/">
    <xsl:copy-of select="ancestor::criterion/>
</xsl:template>

which produces nothing!

I expected it to produce:

<file>
    <criteria>
    </criteria>
</file>

Can someone explain ancestor and descendant axes to me in a more helpful way than the tutorials I have previously read?

Thanks!

  • 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-15T05:41:45+00:00Added an answer on May 15, 2026 at 5:41 am

    And I have the following XSL:

    <xsl:template match="/"> 
        <xsl:copy-of select="ancestor::criterion/> 
    </xsl:template>
    

    which produces nothing!

    As it should!

    ancestor::criterion
    

    is a relative expression, which means that it is evaluated off the current node (matched by the template). But the current node is the document node /.

    So, the above is equivalent to:

    /ancestor::criterion
    

    However, by definition the document node / has no parents (and that means no ancestors), so this XPath expression doesn’t select any node.

    I expected it to produce:

    <file> 
        <criteria> 
        </criteria> 
    </file>
    

    What you probably wanted was:

    //criterion/ancestor::*
    

    or

    //*[descendant::criterion]
    

    The last two XPath expressions are equivalent and select all elements that have a criterion descendant.

    Finally, to produce the output you wanted, here is one possible solution:

    <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="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="root | criterion | format"/>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document, the wanted output is produced:

    <file>
    <criteria>
    </criteria>
    </file>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just learned about XPath and I am wanting to read data from
New to PHP and MySQL, have heard amazing things about this website from Leo
I have some code that pulls HTML from an external source: $doc = new
i have a problem moving a XMLDataprovider Binding with XPath from Xaml to code
I am very new to XSL and XPath. Apologies if this question shows some
So I have some perl code that goes something like: use strict; use XML::XPath;
I have the following Java code working with XPath to get data from XML
I have following HTML which I am trying to read from the DOMNodeList when
In my code, I have $document = DomDocument->loadHTML($someHTML); $xPath = new DOMXPath($document); // //do
I'm new to using XPath so I've been fooling around with an XPath Evaluator

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.