Monday, 17th October 2011
Follow WikiJava on twitter now. @Wikijava

Sibling Check in XSLT

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

The code below iterates to each "jar" and checks if three elements before there was a "cup" element. Executing different commands depending on the case.

Contents

the article

The interesting part of the example is the

<xsl:when test="name(preceding-sibling::*[3]) = 'cup'">

Which checks the name of the element three positions before.

This is executed inside the for-each procedure that iterates through all the jar elements:

<xsl:for-each select="jar">

The example XML

<?xml version="1.0"?>
<document>
	<cup/>
	<cup/>
	<cup/>
	<jar/> 
	<cup/>
	<jar/> 
	<cup/>
	<cup/>
	<jar/> 
</document>

the XSLT code

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
	<xsl:template match="document">
		<xsl:for-each select="jar">
			<xsl:choose>
				<xsl:when test="name(preceding-sibling::*[3]) = 'cup'">
 
				</xsl:when>
				<xsl:otherwise>
 
				</xsl:otherwise>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.