[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Truncating output of a node

Subject: RE: Truncating output of a node
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 19 Apr 2001 16:35:43 +0100
xsl truncate string
At 08:20 PM 4/19/01, you wrote:
I guess another possibility it to output n characters and add '...' at the
end to create the summary. Is that any easier?

Considerably easier, although what you stipulate about how the "." may be good enough , makes the harder problem easier too.


It would be really easy if you didn't care about those embedded <span> elements. Then you could just do:

<xsl:variable name="allowable-length" select="100"/>
<!-- let's say 100 characters is a good number -->

<xsl:template match="summary">
  <xsl:value-of select="substring(., 1, $allowable-length)"/>
  <xsl:if test="string-length(.) &gt; $allowable-length">
    <xsl:text>...</xsl:text>
  </xsl:if>
</xsl:template>

But with those embedded <span> elements, things are harder. I'm not going to tackle the "break on sentences" problem; we'll go with testing on string length.

Maybe something like:

<xsl:variable name="allowable-length" select="100"/>

<xsl:template match="summary">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template name="string-segment">
<!-- outputs as much of the string as will fit -->
<xsl:variable name="string-before">
<xsl:for-each select="$preceding-sibling::*|preceding-sibling::text()">
<!-- counting only elements and text nodes, not comments or
processing-instructions -->
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="length-before" select="string-length($string-before)"/>
<xsl:if test="$length-before &lt; $allowable-length">
<!-- part of this string will fit! we're in business -->
<xsl:variable name="length-left" select="$allowable-length - $length-before"/>
<xsl:value-of select="substring(., 1, $length-left)"/>
<xsl:if test="string-length(.) &gt; $length-left">
<xsl:text>...</xsl:text>
</xsl:if>
</xsl:if>
</xsl:template>


<xsl:template match="summary/text()">
  <xsl:call-template name="string-segment"/>
</xsl:template>

<xsl:template match="summary/span">
  <b> <!-- I'm just making all spans <b>; do what you like -->
    <xsl:call-template name="string-segment"/>
  </b>
</xsl:template>

If I'm thinking about this the right way, it'll work as long as you only have text nodes and span elements in your summary, and you don't have any elements you need to process *inside* the spans, just text nodes.

Note -- untested: sure to be typos and maybe worse. Also -- expect performance to be pretty lame....

Hope this helps!
Wendell

======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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



Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.