views:

782

answers:

1

I am creating a left trim template and I have this below template:

<xsl:template name="str:left-trim">
    <xsl:param name="string" select="''"/>
    <xsl:variable name="tmp" select="substring($string, 1, 1)"/>

    <xsl:if test="$tmp = ' '">
        <xsl:variable name="tmp2" select="substring-after($string, $tmp)"/>
        <xsl:choose>
            <xsl:when test="$tmp2 != ''">
                <xsl:call-template name="str:left-trim">
                    <xsl:with-param name="string" select="$tmp2"/>        
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$tmp2"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
    <xsl:if test="$tmp != ' '">
        <xsl:value-of select="$string"/>
    </xsl:if>
</xsl:template>

if I pass an argument like this:

<xsl:variable name="str-test2">this is a america</xsl:variable>

then my template will work just fine but if I pass an argument like this below then, my template will fail. I think there is something wrong with the break(newline)

    <xsl:variable name="str-test2">            
        this is a america
    </xsl:variable>

do you have any suggestion?

+1  A: 

This works for me. Note I didn't use the str: namespace plus I'm checking for leading newlines.

<xsl:template name="left-trim">
  <xsl:param name="string" select="''"/>
  <xsl:variable name="tmp" select="substring($string, 1, 1)"/>

  <xsl:choose>
    <xsl:when test="$tmp = ' ' or $tmp = '&#xA;'">
      <xsl:call-template name="left-trim">
        <xsl:with-param name="string" select="substring-after($string, $tmp)"/>        
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

HTH, Axel.

axelrose