Author:
bpierre
Version:
0.1
Release Date:
16 Jun 2010
Category:
String Operations

Description

Wraps each line in a custom element.

Thanks to the nl2br Utility for inspiration!

Two params:

string The string to convert.

element-name (optional, default ‘li’) The element name

Example:

<ul>
    <xsl:call-template name="nl2elements">
        <xsl:with-param name="string">
            Line 1
            Line 2
        </xsl:with-param>
        <xsl:with-param name="element-name" select="'li'" />
    </xsl:call-template>
</ul>

Output:

<ul>
    <li>Line 1</li>
    <li>Line 2</li>
</ul>

XSLT

View Raw
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--
	Example:

	<xsl:call-template name="nl2elements">
		<xsl:with-param name="string" select="body" />
		<xsl:with-param name="element" select="'li'" />
	</xsl:call-template>
-->

<xsl:template name="nl2elements">
	<xsl:param name="string" />
	<xsl:param name="element-name">li</xsl:param>
	
	<xsl:if test="normalize-space(substring-before($string,'&#10;')) != ''">
		<xsl:element name="{$element-name}">
			<xsl:value-of select="substring-before($string,'&#10;')" />
		</xsl:element>
	</xsl:if>
	
	<xsl:choose>
		<xsl:when test="contains(substring-after($string,'&#10;'),'&#10;')">
			<xsl:call-template name="nl2elements">
				<xsl:with-param name="string" select="substring-after($string,'&#10;')" />
				<xsl:with-param name="element-name" select="$element-name" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:element name="{$element-name}">
				<xsl:value-of select="substring-after($string,'&#10;')" />
			</xsl:element>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>

Discuss this XSLT Utility

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details