however. Because entries can contain special charachters like æ, ø, å., these are converted to fx "å" But the HTML is Truncated before the characthers are converted. This means that"å" might be cut in the middle, and the correct charachter is not displayed on the webpage
The problem isn't with the StripHtml() or the TruncateString() function. Rather, the richtext editor has already converted special characters for you, such as å
You'll need a more detailed bit of code to be sure you don't truncate your string in the middle of an entity tag because (as you've seen) the simple approach is too simplistic for the actual data.
I'd probably look at making your own extension method to do the truncation on whole words instead, this way, even if they are encoded, it will never split in the middle of a word.
Have a look here for a couple of example methods you could use:
Guess if you wanted to do it all in XSLT you could just split the string on a space, and pull back the first 10 words. I've not tested it, but this should work.
I've tried this myself but I'm not getting the HTML to strip in.
The property alias I have is mainContent however I can't get it working.
Can someone help me out, I'm guessing it's just correcting the body and bodySummary code with I have, but I can't figure it out. Am I missing something?
If you are trying to test on the current node that's being iterated through, you must have the declaration of the body variable inside your for-each loop. This line:
However I'm still not getting any of the HTML from the child node. All I'm getting is "... more" which means it's missing everything before the last part of the links code (...more).
TruncateString
Hi
I use this code, to Truncate the string
<xsl:variable name="body" select="$node/data[@alias = 'Body']" />
<xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml($body) ,100, '...')" disable-output-escaping="yes" />
however. Because entries can contain special charachters like æ, ø, å., these are converted to fx "å" But the HTML is Truncated before the characthers are converted. This means that"å" might be cut in the middle, and the correct charachter is not displayed on the webpage
Thanks
The problem isn't with the StripHtml() or the TruncateString() function. Rather, the richtext editor has already converted special characters for you, such as å
You'll need a more detailed bit of code to be sure you don't truncate your string in the middle of an entity tag because (as you've seen) the simple approach is too simplistic for the actual data.
cheers,
doug.
I'd probably look at making your own extension method to do the truncation on whole words instead, this way, even if they are encoded, it will never split in the middle of a word.
Have a look here for a couple of example methods you could use:
http://stackoverflow.com/questions/1613896/truncate-string-on-whole-words-in-net-c
Matt
thanks for the help. I'm still stuck though :)
lol, what exactly are you stuck on?
Matt
I need a hint for the XSLT, cause I won't be making extensions
Guess if you wanted to do it all in XSLT you could just split the string on a space, and pull back the first 10 words. I've not tested it, but this should work.
Matt
Or this might be better (again, i've not tested it)
Matt
The last one should crop at 100 characters, and then work it's way back to the last space chracter, so you don't split a word.
Matt
It works to some degree. But now every special charahter is just html code.
There is also a minor mistake in your code. Your when tag ends with a choose tag. Just a typo :)
Ooops, thats what I get for typing it in notepad =)
You should just need the disable-output-escaping="yes" putting back on the end then
Matt
wow. this works just perfect. Thanks a lot. Here is the finnehsed code
<xsl:variable name="body" select="umbraco.library:TruncateString(umbraco.library:StripHtml($node/data[@alias = 'Body']), 100, '')" />
<xsl:variable name="bodySummary">
<xsl:choose>
<xsl:when test="contains($body, ' ')">
<xsl:value-of select="substring($body, 0, umbraco.library:LastIndexOf($body, ' '))" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$body" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="concat($bodySummary, '...')" disable-output-escaping="yes" />
Hello, am getting the same thing. Not sure how to apply your fix and am getting errors.
Heres Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<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:param name="MaxNoChars" select="100" />
<xsl:variable name="noOfItems" select="2" />
<xsl:template match="/">
<xsl:for-each select="$currentPage/ancestor-or-self::node//node [@nodeTypeAlias ='9News']/node">
<xsl:sort select="@Name" order="descending"/>
<!-- Position() <= $noOfItems -->
<xsl:if test="position()<= $noOfItems">
<h4><img src="../images/newspaper.png" alt="News" class="left iconpic" />
<a href="{umbraco.library:NiceUrl(@id)}">
<!-- <xsl:value-of select="umbraco.library:FormatDateTime(@createDate, ' dd MMM yyyy ')"/> - -->
<xsl:value-of select="@nodeName" />
</a>
</h4>
<p><!-- TODO: strip HTML & truncate -->
<xsl:value-of disable-output-escaping="yes" select="umbraco.library:TruncateString(umbraco.library:StripHtml(data [@alias = 'bodyText']), $MaxNoChars, '...')" />
<a href="{umbraco.library:NiceUrl(@id)}">more</a>
</p>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Solved this myself and disn't realise.
disable-output-escaping="yes"
Doh!
I've tried this myself but I'm not getting the HTML to strip in.
The property alias I have is mainContent however I can't get it working.
Can someone help me out, I'm guessing it's just correcting the body and bodySummary code with I have, but I can't figure it out. Am I missing something?
Hi JV
If you are trying to test on the current node that's being iterated through, you must have the declaration of the body variable inside your for-each loop. This line:
Otherwise the XSLT doesn't know where to find the mainContent when you are not using $currentPage.
/Kim A
Thanks Kim,
However I'm still not getting any of the HTML from the child node. All I'm getting is "... more" which means it's missing everything before the last part of the links code (...more).
is working on a reply...