Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmix/f0e7a1763b7b3640912146b35726b036 to your computer and use it in GitHub Desktop.
Save bmix/f0e7a1763b7b3640912146b35726b036 to your computer and use it in GitHub Desktop.
XSLT 1.0 Split
<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>
<xsl:template name="split-lf-pad">
<xsl:param name="text"/>
<xsl:param name="bylf" select="'&#xA;'"/>
<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