Adjusting some XSLT for substring code Options
chris
Posted: Tuesday, December 11, 2007 11:32:53 AM
Rank: Devotee

Joined: 4/12/2007
Posts: 93
Location: Amsterdam
Hi all!!
I have some XSLT which I use to automatically get some text for the description meta-tag. The problem I have with it is that is that the script is to 'bruteforce'. Instead of cutting of the text in the middle of a word I would like to end in front (or after) the last word....I understand I have to look for the space in front or after the last word but I have no clue how to do this....any suggestions are highly appreciated!!
this is the code I have right now:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:for-each select="$currentPage [string(data [@alias='umbracoNaviHide']) != '1']">
        <xsl:value-of select="substring(./data [@alias = 'bodyText'],1,175)" disable-output-escaping="yes"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

thanks in advance!
greetings,
Chris
neehouse
Posted: Friday, December 14, 2007 6:12:07 AM

Rank: Umbracoholic

Joined: 7/20/2006
Posts: 1,074
Location: Charleston, West Virginia, United States
Hi Chris,

I would suggest writing an xslt extension with a truncate method that looked for the space or end of a sentence... or even closed html tags properly if affected.

Do you have the ability to create a Class library in .NET?

• 2007/2008 MVP • 2008/2009 MVP • Certified • Licensing • Support • Development • Hosting •
drobar
Posted: Friday, December 14, 2007 2:16:51 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 1,698
Location: KY, USA
Here's what I did to solve this problem the other day.

Code:

<xsl:template match="/">
    <xsl:for-each select="$currentPage/node>
        <xsl:if test="string(data [@alias = 'articleBlurb']) != ''">
            <xsl:value-of select="data [@alias = 'articleBlurb']" disable-output-escaping="yes"/>
        </xsl:if>
        <xsl:if test="string(data [@alias = 'articleBlurb']) = ''">
            <xsl:call-template name="FirstWords">
                <xsl:with-param name="words" select="25"/>
                <xsl:with-param name="text" select="umbraco.library:StripHtml(data [@alias='articleBody'])"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

<!-- ================================================================================== -->

<xsl:template name="FirstWords">
    <xsl:param name="words"/>
    <xsl:param name="text"/>

    <xsl:if test="$words &gt;= 1">
        <xsl:if test="umbraco.library:LastIndexOf($text, ' ') &gt; 0">
            <xsl:value-of select="substring-before($text, ' ')" disable-output-escaping="yes"/><xsl:text> </xsl:text>
        </xsl:if>
        <xsl:if test="umbraco.library:LastIndexOf($text, ' ') &lt;= 0">
            <xsl:value-of select="$text" disable-output-escaping="yes"/>
        </xsl:if>

        <xsl:call-template name="FirstWords">
            <xsl:with-param name="words" select="$words - 1"/>
            <xsl:with-param name="text" select="substring-after($text, ' ')"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>


The reason for the FirstWords template is so that it can be called recursively for each word in the text.

This works well for short word counts but trying to grab the first 2000 words would be slow. If you need many words, another approach should be taken by either rewriting the xslt or creating an inline C# function or creating an xslt extension.

cheers,
doug.

MVP 2007-2009 - Official Umbraco Trainer for North America - Percipient Studios
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.