tags:

views:

65

answers:

1

Hello, I'm trying to create a report in an email and email daemons chop lines if they're over ~ 2040 characters long. I'm using XSLT to build the email report and I need to break up these lines but I still need the link to work.

<xsl:variable name="encoded_url">
  <xsl:value-of select="saxon:string-to-base64Binary(concat(PROTOCOL,'://',URL),'UTF8')" xmlns:saxon="http://saxon.sf.net/"/&gt;
</xsl:variable>
<dd style="{$style-dd}">
  <a title="View URL"
 href="{$baseHref}report?url={$encoded_url}" style="{$style-links}">
 <xsl:call-template name="break-url" >
   <xsl:with-param name="url" select="URL" />
   <xsl:with-param name="length">75</xsl:with-param>
   <xsl:with-param name="lines">999</xsl:with-param>
 </xsl:call-template>
  </a>

This isn't my code and I'm not real familiar with XSL but I've searched on here and on the web and I'm not really seeing an awesome solution.

+1  A: 

This XPath 2.0 expression:

string-join(for $line in tokenize(line,'&#xA;'),
                $length in string-length($line)
            return if ($length > $pMaxLength)
                   then substring(replace($line,
                                          concat('(.{1,',
                                                 $pMaxLength,
                                                 '})'),
                                          '$1&#xA;'),
                                  1,
                                  $length + ceiling($length div $pMaxLength) -1)
                   else $line,
            '&#xA;')

For test, this input:

<line>
line1
line2 line2 line2 line2
line3line3
line4line4
line5line5line5line5line5
</line>

With this XSLT 2.0 stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text"/>
    <xsl:param name="pMaxLength" select="10"/>
    <xsl:template match="/">
        <xsl:value-of
         select="string-join(
                     for $line in tokenize(line,'&#xA;'),
                         $length in string-length($line)
                     return if ($length > $pMaxLength)
                            then substring(replace($line,
                                                   concat('(.{1,',
                                                          $pMaxLength,
                                                          '})'),
                                                   '$1&#xA;'),
                                           1,
                                           $length +
                                           ceiling($length div $pMaxLength) - 1)
                            else $line,
                     '&#xA;')"/>
    </xsl:template>
</xsl:stylesheet>

Output:

line1
line2 line
2 line2 li
ne2
line3line3
line4line4
line5line5
line5line5
line5

Just in case, this XSLT 1.0 stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text"/>
    <xsl:template match="line/text()" name="lines">
        <xsl:param name="pString" select="."/>
        <xsl:param name="pMaxLength" select="10"/>
        <xsl:choose>
            <xsl:when test="contains($pString,'&#xA;')">
                <xsl:call-template name="lines">
                    <xsl:with-param name="pString" 
                     select="substring-before($pString,'&#xA;')"/>
                    <xsl:with-param name="pMaxLength" 
                     select="$pMaxLength"/>
                </xsl:call-template>
                <xsl:call-template name="lines">
                    <xsl:with-param name="pString" 
                     select="substring-after($pString,'&#xA;')"/>
                    <xsl:with-param name="pMaxLength" 
                     select="$pMaxLength"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(substring($pString,1,$pMaxLength),
                                             '&#xA;')"/>
                <xsl:apply-templates 
                 select="current()[string-length($pString) > $pMaxLength]">
                    <xsl:with-param name="pString"
                     select="substring($pString,$pMaxLength+1)"/>
                    <xsl:with-param name="pMaxLength" select="$pMaxLength"/>
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
Alejandro
That is awesome! Thank you!
Thomas Schultz
@Thomas Schultz: You are wellcome!
Alejandro