Sibling Check in XSLTFrom WikiJavabuy 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.
the articleThe 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> |
