Aviram Fireberger
Aviram Fireberger

Reputation: 4178

XSLT - How to add new tag each X characters

I want to add a new tag <br\> to my XML each 10 characters. (without including inner nodes)

For example if my input is:

<main>
   01234567890123
   <a> 
        link        
   </a>
   45678901234567901234
</main>

I expect is to be somthing like:

<main>
   0123456789<br\>0123
   <a> 
        link        
   </a>
   456789<br\>012345679<br\>01234
</main>

I wrote this function:

<!-- Add new "HTML Break" Every X chars  -->
<xsl:template name="Add-HTML-Break-After-X-Chars">
<xsl:param name="text" />
<xsl:param name="max-length" />

<xsl:choose>
  <xsl:when test="string-length($text) > $max-length">

    <!-- Show the X length of the text -->
    <xsl:copy-of select="substring($text,1,$max-length)" />

    <!-- Adding the special tag  -->
    <br/>

    <!-- continue to add the rest of the text  -->
    <xsl:call-template name="Add-HTML-Break-After-X-Chars">
      <xsl:with-param name="text" select="substring($text, $max-length + 1)" />
      <xsl:with-param name="max-length" select="$max-length" />
    </xsl:call-template>


  </xsl:when>
  <xsl:otherwise>

    <!-- Show the text (length is less then X) -->
    <xsl:copy-of select="$text" />

  </xsl:otherwise>
</xsl:choose>

The problem is that I'm getting the output without the inner tags:

<main>
   0123456789<br\>0123
        link        
   45<br\>6789012345<br\>67901234
</main>

I'm using this code to call the template:

    <xsl:call-template name="Add-HTML-Break-After-X-Chars">
      <xsl:with-param name="text" select="main" />
      <xsl:with-param name="max-length" select="10" />
    </xsl:call-template>

I tried also "value-of" instead of "copy-of".

Is there a way I can keep the tags?

If so, how can I keep them and make a "safe" insert of the new tag?

Upvotes: 1

Views: 508

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117175

This is difficult to do in XSLT, because it processes each text node individually, with no information being carried over from the preceding text nodes.

Here's a possible approach:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="main/text()" name="wrap">
    <xsl:param name="text" select="."/>
    <xsl:param name="line-length" select="10"/>
    <xsl:param name="carry">
        <xsl:variable name="lengths">
            <xsl:for-each select="preceding-sibling::text()">
                <length>
                    <xsl:value-of select="string-length()"/>
                </length>
            </xsl:for-each>
        </xsl:variable>
        <xsl:value-of select="sum(exsl:node-set($lengths)/length) mod $line-length"/>
    </xsl:param>

    <xsl:value-of select="substring($text, 1, $line-length - $carry)"/>
    <br/>
    <xsl:if test="$carry + string-length($text) > $line-length">
        <!-- recursive call -->
        <xsl:call-template name="wrap">
            <xsl:with-param name="text" select="substring($text, $line-length - $carry + 1)"/>
            <xsl:with-param name="carry" select="0"/>
        </xsl:call-template>            
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

When this is applied to the following test input:

XML

<main>A123456789B123<a>link1</a>456789C123456789D12345678<a>link2</a>9E123456789F1234567</main>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<main>A123456789<br/>B123<a>link1</a>456789<br/>C123456789<br/>D12345678<a>link2</a>9<br/>E123456789<br/>F1234567</main>

Upvotes: 1

Related Questions