Xpath/XSLT : check if following sibling is a particular node

29,726

Solution 1

You are on the right track. The following-sibling axis evaluates from the context node in the previous step.

  • if you are looking for the first element that is the following-sibling of /Employee/Summary, you would use: /Employee/Summary/following-sibling::*[1].

  • In order to evaluate whether that first following-sibling is an Elections element, you can use an additional predicate filter [self::Elections].

  • Testing for the negation of that, wrap the whole thing in not()

Putting it all together, adjusting your example:

<xsl:if test="not(/Employee/Summary/following-sibling::*[1][self::Elections])">
    <xsl:call-template name="EmployeeRecord"/>
</xsl:if>

Solution 2

I need to write a Xpath condition as follows:

if (NOT(the following sibling(first sibling) of /Employee/Summary is Elections)), then do something.

Use:

not(/Employee/Summary/following-sibling::*[1][self::Elections])

The XPath expression that is specified as argument to the not() function selects any Elections element that is the first following-sibling element of any Summary element that is a child of the top element Employee.

If this doesn't select any node, then the XPath expression above evaluates to true().

Share:
29,726
Richard N
Author by

Richard N

I am a developer with over 6 years of software development experience across various platforms. Currently focusing on cloud technologies such as Salesforce and Workday. Have worked on the Force.com platform for about 3 years now. Love to work on APEX classes/triggers and Visualforce + SOQL. I am very interested in learning new technologies. I have recently started taking an active interest in learning and developing mashups using different technologies such as Google APIs, REST and SOAP API's along with Salesforce I currently hold the following Salesforce certifications Advanced Developer (passed the written exam. Waiting for the programming assignment) Advanced Administrator Developer Administrator Sales Cloud Consultant Service Cloud Consultant I have started to write about Salesforce in general at http://www.decodingthecloud.com/

Updated on October 01, 2020

Comments

  • Richard N
    Richard N almost 4 years

    I have seen questions where following-sibling has been applied based on a node's value but my issue is related to the actual node itself.

    This is the type of XML I have have:

    <Employee>
        <Summary>
            <A>
            <B>
        </Summary>
        <Elections>
        </Elections>
    <Employee>
    

    I need to write a Xpath condition as follows:

    if (NOT(the following sibling(first sibling) of /Employee/Summary is Elections)), then do something.
    

    Currently I have:

    <xsl:if test="(not(following-sibling::Employee/Summary[1]='Earnings'))
        <xsl:call-template name="EmployeeRecord"/>
    </xsl:if>
    

    Please note that I am not checking a node value but the node itself(i.e the node name). Any help in the right direction will be much appreciated.

    • forty-two
      forty-two almost 13 years
      What is the current node? The outlined condition doesn't at all fit with with your current xslt. Can you state in words where you are in the document and what you want to achieve?
  • BurnsBA
    BurnsBA over 11 years
    I hadn't seen the self part before, so for others like me, with a namespace (ns) it is: [self::ns:Elections]