Renato Abreu
Renato Abreu

Reputation: 25

Break text defined length blocks from a long text using xsl

I'm need to break a long text field into one or more repetitions containing part of this long text with defined length (ie. 50 ch) using xsl. INPUT EXAMPLE:

....
<longText>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</longText>

....

I'm expecting the following output: ....
<pieceOfLongTextElement>
<step>1</step>
<text>Lorem Ipsum is simply dummy text of the printing a</text>
</pieceOfLongTextElement>
<pieceOfLongTextElement>
<step>2</step>
<text>nd typesetting industry. Lorem Ipsum has been the </text>
</pieceOfLongTextElement>

and so until the long text length is reached increasing step from 50 to 50 chars...

Thanks.

Upvotes: 1

Views: 216

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

Assuming you can use XSLT 2.0 you can simply break up the string using xsl:analyze-string:

<xsl:template match="longText">
    <xsl:analyze-string select="." regex=".{{1,50}}">
        <xsl:matching-substring>
            <piece step="{position()}">
                <xsl:value-of select="."/>
            </piece>                
        </xsl:matching-substring>
    </xsl:analyze-string>
</xsl:template>

See http://xsltransform.net/pPJ8LWa/1 which outputs

<piece step="1">Lorem Ipsum is simply dummy text of the printing a</piece>
<piece step="2">nd typesetting industry. Lorem Ipsum has been the </piece>
<piece step="3">industry's standard dummy text ever since the 1500</piece>
<piece step="4">s, when an unknown printer took a galley of type a</piece>
<piece step="5">nd scrambled it to make a type specimen book.</piece>

Upvotes: 2

Related Questions