Re: [xsl] substring manipulation to wrap text in an SVG <rect>

Subject: Re: [xsl] substring manipulation to wrap text in an SVG <rect>
From: Jörg Heinicke <joerg.heinicke@xxxxxx>
Date: Mon, 17 Sep 2001 23:36:23 +0200
I wrote a stylesheet for text-wrapping a few weeks ago. It must be adapted a
liitle bit, but it could be a help I think. Important is the template named
"word-wrap".

This is a stylesheet for transforming a Docbook-glossary to a text-file.
Also there is a indenting implemented. So it's maybe interesting for other
people too. For the SVG-text you can simplify it.

Regards,

Joerg


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
    <xsl:output method="text"/>

    <xsl:template match="chapter">
        <xsl:apply-templates select="glossary"/>
    </xsl:template>

    <xsl:template match="glossary">

<xsl:text>##################################################################
##############
</xsl:text>
        <xsl:apply-templates select="title"/>
        <xsl:text>
############################################################################
####

</xsl:text>
        <xsl:apply-templates select="glossdiv|glosslist|glossentry"/>
    </xsl:template>

    <xsl:template match="glossdiv">

<xsl:text>==================================================================
==============
</xsl:text>
        <xsl:apply-templates select="title"/>
        <xsl:text>
</xsl:text>
        <xsl:apply-templates select="para"/>

<xsl:text>==================================================================
==============

</xsl:text>
        <xsl:apply-templates select="glosslist|glossentry"/>
    </xsl:template>

    <xsl:template match="glosslist">
        <xsl:apply-templates select="glossentry"/>
    </xsl:template>

    <xsl:template match="glossentry">
        <xsl:apply-templates select="glossterm|glossdef"/>
    </xsl:template>

    <xsl:template match="glossterm">
        <xsl:for-each select="ancestor::*[not(name()='chapter' or
name()='glossary')]">
            <xsl:text>   </xsl:text>
        </xsl:for-each>
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>
</xsl:text>
    </xsl:template>

    <xsl:template match="glossdef">
        <xsl:apply-templates select="para|glosslist"/>
    </xsl:template>

    <xsl:template match="title">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

    <xsl:template match="para">
        <xsl:call-template name="word-wrap">
            <xsl:with-param name="indent">
                <xsl:for-each select="ancestor::*[not(name()='chapter' or
name()='glossary')]">
                    <xsl:text>   </xsl:text>
                </xsl:for-each>
            </xsl:with-param>
            <xsl:with-param name="tobewrapped" select="normalize-space(.)"/>
        </xsl:call-template>
        <xsl:text>

</xsl:text>
    </xsl:template>

    <xsl:template match="*">
        <xsl:value-of select="text()"/>
    </xsl:template>

    <xsl:template name="word-wrap">
        <xsl:param name="tobewrapped"/>
        <xsl:param name="size" select="0"/>
        <xsl:param name="indent"/>
        <xsl:variable name="maxlength" select="80"/>
        <xsl:choose>
            <xsl:when test="contains($tobewrapped,' ')">
                <xsl:variable name="word"
select="substring-before($tobewrapped,' ')"/>
                <xsl:variable name="length" select="string-length($word)"/>
                <xsl:choose>
                    <xsl:when test="$size=0">
                        <xsl:value-of select="$indent"/>
                        <xsl:value-of select="$word"/>
                        <xsl:call-template name="word-wrap">
                            <xsl:with-param name="tobewrapped"
select="substring-after($tobewrapped,' ')"/>
                            <xsl:with-param name="size"
select="string-length(concat($indent,$word))"/>
                            <xsl:with-param name="indent" select="$indent"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="($size + $length + 1) >
$maxlength">
                                <xsl:text>
</xsl:text>
                                <xsl:value-of select="$indent"/>
                                <xsl:value-of select="$word"/>
                                <xsl:call-template name="word-wrap">
                                    <xsl:with-param name="tobewrapped"
select="substring-after($tobewrapped,' ')"/>
                                    <xsl:with-param name="size"
select="string-length(concat($indent,$word))"/>
                                    <xsl:with-param name="indent"
select="$indent"/>
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="concat(' ',$word)"/>
                                <xsl:call-template name="word-wrap">
                                    <xsl:with-param name="tobewrapped"
select="substring-after($tobewrapped,' ')"/>
                                    <xsl:with-param name="size"
select="$size + 1 + string-length($word)"/>
                                    <xsl:with-param name="indent"
select="$indent"/>
                                </xsl:call-template>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="$size=0">
                        <xsl:value-of select="$indent"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="$size +
string-length($tobewrapped) > $maxlength">
                                <xsl:text>
</xsl:text>
                                <xsl:value-of select="$indent"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text> </xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
                <xsl:value-of select="$tobewrapped"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread