Our Forum has Moved

This site is our old forum and is only here for achive until we get proper 301 redirects setup to make Google happy.

Please use our new community site - Our Umbraco - which contains an improved forum, documentation wiki, package repository and a member locator.

Go to Our Umbraco now

Learn everything about Umbraco
Adjusting some XSLT for substring code Options
chris
Posted: Tuesday, December 11, 2007 11:32:53 AM

Rank: Aficionado

Joined: 4/12/2007
Posts: 112
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,150
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 •
Best Case Technologies, LLC
• Umbraco Licensing • Support Packages • Web and Application Development • Hosting • Multi-Lingual Site •
drobar
Posted: Friday, December 14, 2007 2:16:51 PM

Rank: Umbracoholic

Joined: 9/8/2006
Posts: 2,020
Location: MA, USA; Cambridge, UK
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 - Percipient Studios - Percipient Blog
HFloyd
Posted: Saturday, June 20, 2009 7:52:18 PM

Rank: Fanatic

Joined: 7/19/2006
Posts: 226
Location: New York, NY, USA
Doug,

I was successfully using your FirstWords code, until today when it was causing the page to stop functioning.

The text I was truncating is from an RSS feed where I have a macro that takes various parameters, including how many words of the post text should be displayed. The post in question is returning this as the HTMLStripped (description):
Code:

An email I received from the Free Press Action Fund ... It takes just a moment to show your support to the FCC. The issue of Net Neutrality affects anyone who uses the web for entertainment – or business. The Email: &quot;I'm a guy who sees nothing good having come from the Internet. Period.&quot; Michael Lynton CEO of Sony Pictures


When I put in debug code to narrow down where the code stopped working, I see it is breaking right after "Email:", where the ampersand is. I don't know why it should cause a problem. It seems to me that "&quot;I'm" should just be recognized as a single word...

I have the macro set to return 47 words, but it is only returning 41 words:
Code:
An email I received from the Free Press Action Fund ... It takes just a moment to show your support to the FCC. The issue of Net Neutrality affects anyone who uses the web for entertainment – or business. The Email: ...


If I set the macro to 100 words, which was what my original preference was, it seems to run too long or something because in Firefox the whole page errors with:
XML Parsing Error: no element found
Location: http://www.wholewebimpact.com/test.aspx
Line Number 1, Column 1:


Have you encountered this issue? Any tips on how to resolve it?

Thanks!

Heather


Whole Web Impact | Floyd Innovations LLC | Heather Floyd's Blog

The forum has moved

This forum is no longer in use, so you can't reply to this message - please go to Our Umbraco

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.