How to use a counter in a XSL Loop (xsl:for-each)

To use a counter in a XSL For-Each block, you can use the position() function, the Counter starts at value 1.

Sample XSL

<xsl:for-each select="Order/Products/Product">
    <position><xsl:value-of select="position()"></xsl:value-of></position>
</xsl:for-each>

see also MSDN position Function

Please rate this snippet

How to trim a string using XSL 1.0

Since XSL 1.0 dows not have inbuild Trimming functions for Strings, you need to define your own templates.
You can use the Trim Templates shown in the codesnippet below.

Sample XSL

<xsl:variable name="whitespaceCharacters" select="'&#09;&#10;&#13; '" />
    
    <!-- Trim Right side of the String -->
    <xsl:template name="TrimRight">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        
        <xsl:variable name="length" select="string-length($input)" />
        <xsl:if test="string-length($input) &gt; 0">
            <xsl:choose>
                <xsl:when test="contains($trim, substring($input, $length, 1))">
                    <xsl:call-template name="TrimRight">
                        <xsl:with-param name="input" select="substring($input, 1, $length - 1)" />
                        <xsl:with-param name="trim" select="$trim" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$input" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    
    <!-- Trim Left side of the String -->
    <xsl:template name="TrimLeft">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        
        <xsl:if test="string-length($input) &gt; 0">
            <xsl:choose>
                <xsl:when test="contains($trim, substring($input, 1, 1))">
                    <xsl:call-template name="TrimLeft">
                        <xsl:with-param name="input" select="substring($input, 2)" />
                        <xsl:with-param name="trim" select="$trim" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$input" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    
    <!-- Trim both sides of the String -->
    <xsl:template name="Trim">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        <xsl:call-template name="TrimRight">
            <xsl:with-param name="input">
                <xsl:call-template name="TrimLeft">
                    <xsl:with-param name="input" select="$input" />
                    <xsl:with-param name="trim" select="$trim" />
                </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name="trim" select="$trim" />
        </xsl:call-template>
    </xsl:template>

Please rate this snippet

How to get element with the highest ID using XSL

To get element with the highest ID using XSL you can use the following snippet.

XML:

<rootnode>
    <node>
        <id>3</id>
    </node>
    <node>
        <id>2</id>
    </node>
    <node>
        <id>1</id>
    </node>
	<node>
        <id>5</id>
    </node>
    <node>
        <id>4</id>
    </node>
</rootnode>

XSL:

<xsl:variable name="MaxNodeId"><xsl:value-of select="/rootnode/node[not(/rootnode/node/id > id)]/id"/></xsl:variable>

Please rate this snippet

How to check if String is Null or Empty in XSL

To check if String is Null or Empty in XSL you can use the following snippet.

Sample XSL

<xsl:choose>
    <xsl:when test="fieldToTest != ''">
        <xsl:value-of select="Field1Value" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="Field2Value" />
    </xsl:otherwise>
</xsl:choose>

Please rate this snippet

How to do String Replace in XSL 1.0

To do String Replace in XSL 1.0 you can use the following snippet.

<xsl:template name="StringReplace">
  <xsl:param name="input" />
  <xsl:param name="search" />
  <xsl:param name="replace" />
  <xsl:choose>
    <xsl:when test="contains($input, $search)">
      <xsl:value-of select="substring-before($input,$search)" />
      <xsl:value-of select="$replace" />
      <xsl:call-template name="StringReplace">
        <xsl:with-param name="input"
        select="substring-after($input,$search)" />
        <xsl:with-param name="search" select="$search" />
        <xsl:with-param name="replace" select="$replace" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$input" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

The Template can be invoked as:

<xsl:variable name="newInput">
  <xsl:call-template name="StringReplace">
    <xsl:with-param name="input" select="$input" />
    <xsl:with-param name="search" select="string1" />
    <xsl:with-param name="replace" select="string2" />
  </xsl:call-template>
</xsl:variable>

Please rate this snippet