Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

xpath

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.

like image 948
Richard N Avatar asked Sep 28 '11 22:09

Richard N


People also ask

Where to use following sibling in XPath?

We can use the concept of following-sibling in xpath for identifying elements in Selenium. It identifies the siblings of the context node. The siblings should be located at the equal level of the existing node and should have the same parent.

What is following sibling in XSLT?

The following-sibling axis indicates all the nodes that have the same parent as the context node and appear after the context node in the source document.

What is preceding-sibling and following sibling?

The only difference in functionality is that of preceding. So, here, in contrast to Following-Sibling, you get all the nodes that are siblings or at the same level but are before your current node. The syntax for using Preceding-Sibling is: //tagname[@attribute='value']//preceding-sibling::tagname.


2 Answers

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>
like image 51
Mads Hansen Avatar answered Oct 03 '22 00:10

Mads Hansen


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().

like image 26
Dimitre Novatchev Avatar answered Oct 03 '22 00:10

Dimitre Novatchev