Created
August 10, 2011 17:02
-
-
Save emiliano-poggi/1137424 to your computer and use it in GitHub Desktop.
XSLT 1.0 Split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsl:template name="split"> | |
<xsl:param name="text"/> | |
<xsl:param name="by"/> | |
<xsl:param name="normalize" select="false()"/> | |
<xsl:variable name="l" select="substring-before($string, $by)"/> | |
<xsl:variable name="r" select="substring-after($string, $by)"/> | |
<xsl:choose> | |
<xsl:when test="$l and $normalize"> | |
<xsl:value-of select="normalize-space($l)" /> | |
</xsl:when> | |
<xsl:when test="$l"> | |
<xsl:value-of select="$l" /> | |
</xsl:when> | |
<xsl:when test="not($l) and $normalize"> | |
<xsl:value-of select="normalize-space($string)" /> | |
</xsl:otherwise> | |
<xsl:otherwise> | |
<xsl:value-of select="$string" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
<xsl:if test="$r"> | |
<xsl:call-template name="split-string-pad"> | |
<xsl:with-param name="string" select="$r" /> | |
<xsl:with-param name="by" select="$by" /> | |
<xsl:param name="normalize" select="$normalize"/> | |
</xsl:call-template> | |
</xsl:if> | |
</xsl:template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsl:template name="split-lf-pad"> | |
<xsl:param name="text"/> | |
<xsl:param name="bylf" select="'
'"/> | |
<xsl:param name="pad"/> | |
<xsl:variable name="l" select="substring-before($string, $bylf)"/> | |
<xsl:variable name="r" select="substring-after($string, $bylf)"/> | |
<xsl:choose> | |
<xsl:when test="$l"> | |
<xsl:variable name="spaces"> | |
<xsl:call-template name="padding"> | |
<xsl:with-param name="times" | |
select="$pad - string-length(normalize-space($l))"/> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:value-of select="concat(normalize-space($l),$spaces)" /> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="normalize-space($string)" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
<xsl:if test="$r"> | |
<xsl:call-template name="split-string-pad"> | |
<xsl:with-param name="string" select="$r" /> | |
</xsl:call-template> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template name="padding"> | |
<xsl:param name="times" select="0"/> | |
<xsl:param name="spaces" select="''"/> | |
<xsl:choose> | |
<xsl:when test="$times>0"> | |
<xsl:call-template name="padding"> | |
<xsl:with-param name="times" select="$times - 1"/> | |
<xsl:with-param name="spaces" select="concat($spaces,' ')"/> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="$spaces"/> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment