增強喬恩HORSTMANN的答案,包括LPAD和RPAD
<xsl:template name="rpad">
<xsl:param name="str"/>
<xsl:param name="len" select="0" />
<xsl:param name="chr" select="' '" />
<xsl:call-template name="pad">
<xsl:with-param name="str" select="$str" />
<xsl:with-param name="len" select="$len" />
<xsl:with-param name="chr" select="$chr" />
</xsl:call-template>
</xsl:template>
<xsl:template name="lpad">
<xsl:param name="str"/>
<xsl:param name="len" select="0" />
<xsl:param name="chr" select="' '" />
<xsl:call-template name="pad">
<xsl:with-param name="str" select="$str" />
<xsl:with-param name="len" select="$len" />
<xsl:with-param name="chr" select="$chr" />
<xsl:with-param name="justify" select="'right'" />
</xsl:call-template>
</xsl:template>
<xsl:template name="pad">
<xsl:param name="str"/>
<xsl:param name="len" select="0" />
<xsl:param name="chr" select="' '" />
<xsl:param name="justify" select="'left'"/>
<xsl:choose>
<xsl:when test="string-length($str) < $len">
<xsl:variable name="newStr">
<xsl:choose>
<xsl:when test="$justify = 'left'">
<xsl:value-of select="concat($str,' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(' ',$str)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:call-template name="pad">
<xsl:with-param name="len" select="$len"/>
<xsl:with-param name="justify" select="$justify" />
<xsl:with-param name="chr" select="$chr" />
<xsl:with-param name="str" select="$newStr" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
要正確墊字符串 'myvalue的' 用空格到20個字符使用:
<xsl:call-template name="rpad">
<xsl:with-param name="str" select="'myvalue'" />
<xsl:with-param name="len" select="20" />
</xsl:call-template>
要離開墊字符串 'myvalue的'使用空格至36個字符使用:
<xsl:call-template name="lpad">
<xsl:with-param name="str" select="'myvalue'" />
<xsl:with-param name="len" select="36" />
</xsl:call-template>
或通話簿直接指定文本理由期望的(即,左或右)
<xsl:call-template name="pad">
<xsl:with-param name="str" select="'myvalue'" />
<xsl:with-param name="len" select="36" />
<xsl:with-param name="justify" select="'right'" />
</xsl:call-template>
你爲什麼不學習xslt ?! – 2009-08-06 11:50:41