Getting parent's parent from current node in Xpath 2.0

40,877

Solution 1

The problem is in /[. You can change it to

../../self::*[@status='current']

Solution 2

A simpler solution than those from choroba and Hansen is

../..[@status='current']

Solution 3

You could also use the following:

parent::*/parent::*[@status='current']

Solution 4

With Xpath 2.0:

 ../../@status eq 'current'

With XPath 1.0 and XPath 2.0:

 ../../@status = 'current'
Share:
40,877
meder omuraliev
Author by

meder omuraliev

I began learning front end web development around 04-05, started my first program around 06 with PHP. Currently, I am a Web technologist specializing in full stack development and linux administration specializing with the LAMP stack ( HTML5, CSS3, PHP, Apache, MySQL). I also like to dabble in node.js, meteorjs, Python, django and in general like to mess with new technology/stacks. LinkedIn | [email protected]

Updated on August 07, 2020

Comments

  • meder omuraliev
    meder omuraliev almost 4 years

    I always seem to have trouble with xpath axis expressions...

    In some expressions I have used ../ to refer to the parent node, but is that not valid for test expressions? Or is my syntax just wrong?

    <xsl:when test="../../[@status='current']">
    

    My goal is to apply an attribute inside the xsl:when IF the parent's parent has a status attribute with a value of 'current'.

    EDIT: self::parent/parent[@status='current'] is a valid xpath expression and may be what I want, can anyone confirm? I might not be going far enough.