How To Skip The First X Nodes During foreach Loop In XSLT


Have you ever wanted to skip the first X nodes during a foreach loop?  In my case, I was taking a flat CSV file and turning it into a generic XML document.  I was then using XSLT to transform that document into a meaningful XML document representing Customer.  The first 2 rows of my CSV file, were header and file information only, therfore I did not want to process them.  Rather than hard code the rows to skip in a pre-processor for the flat file, I can just transform the data to my final document, and then skip X rows in XSLT.  I was using XSLT anyhow to create my customer document, so it was just as easy to put the logic there.




<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
  <xsl:template match="/">
      <xsl:variable name="myRootNode" select="."/>
      <xsl:for-each select="$myRootNode/MyNodes/Node">
        <xsl:variable name="myNode" select="."/>
        <xsl:if test="position() > 2">
             Do Your Other XSLT Stuff Here ...



This will skip the first 2 nodes I encounter, which represents the first 2 header rows that I had in my document.

Add comment

  Country flag

  • Comment
  • Preview