HTML Character Truncation
This is an open discussion with 10 replies, filed under XSLT.
Search
Nice.
Hi Allen,
Great template. Do you have any idea when your to-do items might be done? I could make use of this utility on a site I'm currently working on if those items were addressed.
Hi Allen,
Your truncate utility works a treat, thankyou so much! Now I am implementing it on a page that has 5 news item divs with varying amounts of text that are grabbed by
<xsl:apply-templates select="news/entry[position()=1]"/>
I only need to truncate the first two. Now it works on the first but not on the second. Can the utility not work multiple times on one page?
Many thanks
@almacmillan: I forgot to consider the instance where there would be multiple of the same element, the oversight in question is my use of preceding::text()[ancestor::body]
which would match all previous elements that is a children of a body
node.
I will need to update the XSLT so that it only matches the context
's body node.
I've updated the XSLT on gist. This should now work with multiple entries. Note that that the apply-template
select pattern to invoke the truncation has changed:
It used to be:
<xsl:apply-templates select="path/to/your/body/*" mode="truncate"/>
It is now:
<xsl:apply-templates select="path/to/your/body" mode="truncate"/>
Allen, when I copy the new code from your gist into a new Symphony utility I get this error:
This document is not well formed. The following error was returned: loadXML(): Comment not terminated in Entity, line: 4
The reason for this is the way your first comment block is formatted. Tweaking it to look like this does the job:
<!-- Author: Allen Chang Version: 1.1 Description: Truncates by character limit while gracefully retains HTML Usage: <xsl:apply-templates select="path/to/your/body" mode="truncate"/> -->
Stupid mistake - double dash in comment blocks are invalid. Code on gist is updated.
I've updated the code to allow for 2 parameters:
<xsl:apply-templates match="path/to/body" mode="truncate"> <xsl:param name="limit" select="100"/> <xsl:param name="suffix" select="'→'"/> </xsl:apply-templates>
The default for limit is 250
and the default suffix is …
.
Allen, you are officially my hero ;-)
Even though you are my hero Allen, here's my take on this.
Create an account or sign in to comment.
This is my take on the truncation problem. It's quite simple and elegant. It uses a similar concept as the Ninja HTML Technique. It's written using 3 templates and no extensions. To invoke the code, simply call it like you would with the HTML Manipulation utility:
To Do: