How to Select the First/Last Sibling in XSLT

(0 votes, average 0 out of 5)

From my experiences with using XSL to transform XML documents, I frequently find the need to test the name of either the sibling directly following or directly preceding a given node.  Unfortunately, there is not XPATH expression that provides this functionality.  The following code should give you what you are looking for, however.

Check to see if the first preceding sibling is "car":

<xsl:if test="name(preceding-sibling::*[1]) = 'car'">
<!-- code to execute -->
</xsl:if>

Likewise, to test to see if the first following sibling is "car" use the following:

<xsl:if test="name(following-sibling::*[1]) = 'car'">
<!-- code to execute -->
</xsl:if>

Partner Links:
Last Updated on Tuesday, 20 July 2010 23:19  
Related Articles

» How to Refurbish Your NES (so you do not have to blow on the games anymore)

The NES is one of the most successful systems to exist. Unfortunately, Nintendo didn't plan to have people still play the NES for this long. I'm here to tell you how to clean the games, get rid of that dreaded blinking screen, and how to clean your controller on the inside so it has the perfect response that it used to have. You dusted off your NES, hooked it up, ready to play Contra, turn it on, and you get the dreaded blinking red/blue/green/whatever color screen, depending on your model....

» How to Fix an Atari 2600: From Top to Bottom

Believe it or not, the Atari 2600 is one beast of a console, and very rarely dies. Its like the annoying brother you kick in the pants, and he still comes back. Here are some notes to keep your console looking fresh, clean, and working perfect. Don't forget, you need to use either the RF box that comes with the atari, or you can buy an RCA to Coax adapter. An automatic RF switching box will *not* work, because the atari signal isn't strong enough.Out of all of the consoles I like to work...

» Merge Two Versions of A Table Together in a Database

When keeping multiple versions of a website, or perhaps migrating a website to a new environment, it is often necessary to merge any changes between the two databases. The databases have the same structure, but each has new, unique content which needs to be added to the other. The following tutorial uses syntax for MySQL, however it should be applicable to other major database schemas. The following example assumes that the master database where we want all of the changes to reside is called...