Question

I am stuck with XSLT 1.0 making a hyphenation script for verbatim output to PDF. (note it is PDF so I can't use clever CSS to hellp me out. I would like to be able to access both text() nodes and child text() nodes to compare stringlength as part of the script.

For example:

<programlisting>This text will be displayed without word-wrap
I can measure length of string between end line chars '&#10;'
and break long lines. That works great until

<emphasis>this gets added</emphasis> then my counting loop doesn't
count the text in the emphasis tags.
</programlisting>

I am looping through the text() using a template called screen. It detects the end of line character and decides if the line is too long. Long lines are broken and the remainder recursed through the template until the remainder is less than the max line length... then onto the next line of text... ... that works great, but now i have some children in the and I can't figure out how I can access the text() and any child node's text() at the same time to calculate string length.

Sorry for the long example code...

The template to match looks like this...

<xsl:template match="programlisting/text()"> 
   <xsl:variable name="max_width">100</xsl:variable>
   <xsl:variable name="min_width">80</xsl:variable>
          <xsl:call-template name="screen">
            <xsl:with-param name="text" select="."/>
            <xsl:with-param name="max_width" select="$max_width"/>
            <xsl:with-param name="min_width" select="$min_width"/>
          </xsl:call-template>
</xsl:template>

The template to decide if string before linebreak is too long:

<xsl:template name="screen"> 
    <xsl:param name="text" select="."/>
    <xsl:param name="max_width"/>
    <xsl:param name="min_width"/>
    <xsl:variable name="fixed_text"> <!-- add end linebreak if missing -->
        <xsl:choose>
            <xsl:when test="substring($text,string-length($text),1) = '&#10;'">
                <xsl:value-of select="$text"/>  
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat($text,'&#10;')"/>  
            </xsl:otherwise>
        </xsl:choose> 
     </xsl:variable>
     <xsl:variable name="first_line">
                <xsl:value-of select="substring-before(concat($fixed_text,'&#10;'),'&#10;')"/>
    </xsl:variable>
    <xsl:variable name="other_lines">
                <xsl:value-of select="substring-after($fixed_text,concat($first_line,'&#10;'))"/>
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="string-length(normalize-space($first_line)) &lt; 1"><!-- blank line (just trim and copy)-->
            <xsl:value-of select="concat($verbatim_padding,substring($first_line,1,100),'&#10;')"/>
        </xsl:when>
        <xsl:when test="string-length($first_line) &lt; 100"> <!-- short line (just copy)-->
            <xsl:value-of select="concat($verbatim_padding,$first_line,'&#10;')"/>
        </xsl:when>
        <xsl:otherwise>
            <!--  Line is too long!!  -->
            <xsl:variable name="wrapped_lines">
               <xsl:call-template name="break_line"> 
                    <xsl:with-param name="long_string" select="$first_line"/>
                    <xsl:with-param name="max_chars" select="100"/>
                    <xsl:with-param name="min_chars" select="80"/>
                    <xsl:with-param name="break_chars" select="' ,/;'"/>
                    <xsl:with-param name="linebreak_string" select="'~'"/>
                </xsl:call-template> 
            </xsl:variable>
            <xsl:value-of select="concat($verbatim_padding,$wrapped_lines)"/>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="string-length($other_lines) &gt; 0">
          <xsl:call-template name="screen">
            <xsl:with-param name="text" select="$other_lines"/>
            <xsl:with-param name="max_width" select="$max_width"/>
            <xsl:with-param name="min_width" select="$min_width"/>
          </xsl:call-template>
    </xsl:if>
</xsl:template>

The template to break up long lines:

<xsl:template name="break_line"> 
    <xsl:param name="long_string"/>
    <xsl:param name="max_chars"/> <!--max chars allowed on a line -->
    <xsl:param name="min_chars"/> <!--max char position foa soft linebreak (else we hard break at the max chars!) -->
    <xsl:param name="break_chars"/>   <!-- chars used for soft breaking -->
    <xsl:param name="linebreak_string"/>  <!-- add this to end of a broken line -->
    <xsl:choose>
      <xsl:when test="(string-length($long_string) &lt; $max_chars) or (string-length($long_string) &lt; $min_chars) or (string-length($long_string) &lt; 1)">
          <xsl:value-of select="concat($long_string,'&#10;')"/>
       </xsl:when>
      <xsl:otherwise>
            <xsl:variable name="trim_x_by">
                    <xsl:call-template name="CheckLastChar">
                        <xsl:with-param name="string" select="$long_string"/>
                        <xsl:with-param name="start" select="$max_chars"/>
                        <xsl:with-param name="stop" select="$min_chars"/>
                        <xsl:with-param name="break_chars" select="$break_chars"/>
                        <xsl:with-param name="max_chars" select="$max_chars"/>
                     </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="first_x_chars"><xsl:value-of select="substring($long_string,1,$trim_x_by)"/></xsl:variable>
            <xsl:variable name="remaining_chars"><xsl:value-of select="substring-after($long_string,$first_x_chars)"/></xsl:variable>

            <xsl:value-of select="concat($first_x_chars,' ',$linebreak_string)"/>

            <xsl:call-template name="break_line"> 
                <xsl:with-param name="long_string" select="$remaining_chars"/>
                <xsl:with-param name="max_chars" select="$max_chars"/>
                <xsl:with-param name="min_chars" select="$min_chars"/>
                <xsl:with-param name="break_chars" select="$break_chars"/>
                <xsl:with-param name="linebreak_string" select="$linebreak_string"/>
            </xsl:call-template> 
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>

And finally the recursive checking of string chars to see if its a safe place to line-break (soft break)

   <xsl:template name="CheckLastChar">
        <xsl:param name="string"/>
        <xsl:param name="stop" select="1"/>
        <xsl:param name="start" select="string-length($string)"/>
        <xsl:param name="count" select="$start"/>
        <xsl:param name="break_chars"/>
        <xsl:param name="max_chars"/>
        <xsl:choose>
          <xsl:when test="($count &lt; $stop) or (string-length($string) &lt; $stop)"> 
                <!-- gone so far into the line that a linebreak would look weird! So return the max-length and that will
                force a 'hard' linebreak exactly at the max-length point regardless of the character -->
                <xsl:value-of select="$max_chars"/> 
          </xsl:when>
          <xsl:otherwise>
                <xsl:variable name="last_char"><xsl:value-of select="substring($string,$count,1)"/></xsl:variable>
                <xsl:choose>
                  <xsl:when test="contains($break_chars,$last_char)">
                        <xsl:value-of select="$count"/>
                  </xsl:when>
                 <xsl:otherwise>
                        <!-- keep looking -->
                        <xsl:call-template name="CheckLastChar">
                            <xsl:with-param name="string" select="$string"/>
                            <xsl:with-param name="stop" select="$stop"/>
                            <xsl:with-param name="start" select="$start"/>
                            <xsl:with-param name="count" select="$count - 1"/>
                            <xsl:with-param name="break_chars" select="$break_chars"/>
                            <xsl:with-param name="max_chars" select="$max_chars"/>
                        </xsl:call-template>
                  </xsl:otherwise>
                </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
Was it helpful?

Solution

To get all of the text in an element (including its descendants' text), just use string(Element):

<xsl:template match="programlisting"> 
   <xsl:variable name="max_width">100</xsl:variable>
   <xsl:variable name="min_width">80</xsl:variable>
          <xsl:call-template name="screen">
            <xsl:with-param name="text" select="string(.)"/>
            <xsl:with-param name="max_width" select="$max_width"/>
            <xsl:with-param name="min_width" select="$min_width"/>
          </xsl:call-template>
</xsl:template>

Note the removal of text() from the match attribute.

XPath Spec

The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.

The string() function converts an object to a string as follows: A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top