User Tools

Site Tools


notes:xslt:strings

Strings in XSLT

String length

<!-- Example #1 -->
<xsl:if test="string-length(FIELD) &gt; 0">...</xsl:if>
 
<!-- Example #2 -->
<xsl:if test="string-length(@var) &gt; 0">...</xsl:if>

Substrings

<!-- Example #1 -->
<xsl:value-of select="substring($var,string-length($var) - $length + 1)"/>
 
<!-- Example #2 -->
<xsl:value-of select="substring($Date1,7,4)"/>
 
<!-- Example #3 -->
<xsl:when test="substring-after(@url,'.aspx?')">
    <xsl:variable name="Rank" select="substring-after(@url, 'rank=')"/>
    <xsl:variable name="Text" select="/siteMap/ranks/rank[@id=$Rank]/@text"/>
    <xsl:value-of select="$Text" />
</xsl:when>

Repeat a string

Repeat a given string (this template comes from the XSLT Standard Library v1.1):

<!-- the template -->
<xsl:template name="generate-string">
    <xsl:param name="text"/>
    <xsl:param name="count"/>
    <xsl:choose>
        <xsl:when test="string-length($text) = 0 or $count &lt;= 0"/>
        <xsl:otherwise>
  	<xsl:value-of select="$text"/>
  	<xsl:call-template name="generate-string">
  	  <xsl:with-param name="text" select="$text"/>
  	  <xsl:with-param name="count" select="$count - 1"/>
  	</xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
</xsl:template>
 
<!-- the usage -->
<xsl:call-template name="generate-string">
  <xsl:with-param name="text" select="'&#160;&#160;'"/>
  <xsl:with-param name="count" select="3"/>
</xsl:call-template>

Capitalize words

Example #1:

<xsl:for-each select="title/*">
    <xsl:call-template name="string-capitalize">
        <xsl:with-param name="string" select="."/>
    </xsl:call-template>
</xsl:for-each>

Input:

<title>
  <w>foundations</w>
  <w>of</w>
  <w>algorithms</w>
</title>

Output:

Foundations Of Algorithms

Example #2:

<xsl:for-each select="authors/author">
    <xsl:for-each select="*">
        <xsl:call-template name="string-capitalize">
            <xsl:with-param name="string" select="."/>
        </xsl:call-template>
    </xsl:for-each>
    <xsl:if test="position() &lt; last()">,&#160;</xsl:if>
</xsl:for-each>

Input:

<authors>
  <author>
    <w>simon</w>
    <w>harris</w>
  </author>
  <author>
    <w>james</w>
    <w>ross</w>
  </author>
</authors>

Output:

Simon Harris, James Ross

The template:

<!-- capitalize a word and attach a space if this is not the last word in a sentence -->
<xsl:template name="string-capitalize" >
    <xsl:param name="string"/>
    <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:value-of select="translate(substring($string,1,1),$lowercase,$uppercase)"/>
    <xsl:value-of select="substring($string,2)"/>
    <xsl:if test="position() &lt; last()">&#160;</xsl:if>
</xsl:template>

String padding

Right justify and prepend the value with the padChar character:

<xsl:template name="prepend-pad">
    <xsl:param name="padChar"> </xsl:param>
    <xsl:param name="padVar"/>
    <xsl:param name="length"/>
    <xsl:choose>
        <xsl:when test="string-length($padVar) &lt; $length">
            <xsl:call-template name="prepend-pad">
                <xsl:with-param name="padChar" select="$padChar"/>
                <xsl:with-param name="padVar" select="concat($padChar,$padVar)"/>
                <xsl:with-param name="length" select="$length"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring($padVar,string-length($padVar) - $length + 1)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

usage:

<xsl:call-template name="prepend-pad">
    <xsl:with-param name="padChar">
        <xsl:value-of select="0"/>
    </xsl:with-param>
    <xsl:with-param name="padVar">
        <xsl:value-of select="$i"/>
    </xsl:with-param>
    <xsl:with-param name="length">
        <xsl:value-of select="4"/>
    </xsl:with-param>                        
</xsl:call-template>

Replace a string

  <xsl:template name="string-replace" >
    <xsl:param name="text"/>
    <xsl:param name="from"/>
    <xsl:param name="to"/>
    <xsl:choose>
      <xsl:when test="contains($text,$from)">
        <xsl:value-of select="substring-before($text,$from)"/>
        <xsl:copy-of select="$to"/>
        <xsl:call-template name="string-replace">
          <xsl:with-param name="string" select="substring-after($text,$from)"/>
          <xsl:with-param name="from" select="$from"/>
          <xsl:with-param name="to" select="$to"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

usage:

<xsl:call-template name="string-replace">
    <xsl:with-param name="text" select="@url"/>
    <xsl:with-param name="from" select="'~/'"/>
    <xsl:with-param name="to" select="'http://www.wbswiki.com/'" />
</xsl:call-template>

Extract a filename

<xsl:variable name="file-name">
  <xsl:call-template name="extract-filename">
    <xsl:with-param name="full-path" select="@path" />
  </xsl:call-template>
</xsl:variable>
 
<xsl:template name="extract-filename">
 
  <xsl:param name="full-path" />
 
  <xsl:choose>
    <xsl:when test="contains($full-path, '/')">
      <xsl:call-template name="extract-filename">
        <xsl:with-param name="full-path" select="substring-after($full-path, '/')" />      
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$full-path" />
    </xsl:otherwise>
  </xsl:choose>
 
</xsl:template>
notes/xslt/strings.txt · Last modified: 2017/05/02 by leszek