Custom Search
Logiclabz
  • Home
  • XML/XSLT
  • Get Previous Node / preceding-sibling in XSLT (XPath)

Get Previous Node / preceding-sibling in XSLT (XPath)

  

"preceding-sibling::<<Node Name>>" can be use to get all the previous siblings. But to get the just before sibiling syntax is like below "preceding-sibling::<<Node Name>>[<<count>>]"

Sample XML doc

<Parents>
       <Child id="1" name="Yogesh1" mode="1" />
       <Child id="2" name="Yogesh2" mode="1" />
       <Child id="3" name="Yogesh3" mode="2" />
       <Child id="4" name="Yogesh4" mode="2" />
       <Child id="5" name="Yogesh5" mode="2" />
</Parents>
Sample XSLT Template for preceding-sibling
<xsl:template match="Child">
       <xsl:if test="@mode != preceding-sibling::Child[1]/@mode">
              <xsl:value-of select="Change in mode Attribute" />
       </xsl:if>
       Name:<xsl:value-of select="@name" />
       Id:<xsl:value-of select="@id" />
</xsl:template>



  


Leave a reply


Comments

  • Donna Fitzgerald says:
    Sep 08, 10

    This code did not work as expected, even when I copied it straight into XMLSpy as is. The XSLT for select="Change in mode Attribute" should have inner single quotes. When I ran it, it generated no output.



Do you like this post?