Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 139449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:26:13+00:00 2026-05-11T07:26:13+00:00

I have a question for the clever people of the SO community. Below is

  • 0

I have a question for the clever people of the SO community.

Below is a snippet of XML generated by the Symphony CMS.

   <news>         <entry>             <title>Lorem Ipsum</title>             <body>                 <p><strong>Lorem Ipsum</strong></p>                 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada auctor magna. Vivamus urna justo, pulvinar nec, sagittis malesuada, accumsan in, massa. Quisque mi purus, gravida eget, ultricies a, porta in, sem. Maecenas justo elit, elementum vel, feugiat vulputate, pulvinar nec, velit. Fusce vel ante et diam bibendum euismod. Nunc vel nulla non lorem dignissim placerat. Nulla magna massa, auctor et, tempor nec, auctor sit amet, turpis. Quisque odio lacus, auctor at, posuere id, suscipit eget, dui. Phasellus aliquam. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin varius. Phasellus cursus. Cras mattis adipiscing turpis. Sed.</p>                 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada auctor magna.</p>             </body>         </entry>     </news> 

What I need to do is take a portion of the <body> element, based on a specified length, for display in the blog style of:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed malesuada auctor magna. Vivamus urna justo, pulvinar nec, sagittis malesuada, accumsan in, massa. Quisque mi purus, gravida eget, ultricies a, porta in, sem… more

…where more is a link to the full news item. I know I can select specific paragraphs and I also know I can use the substring function to bring a specified number of characters. However, I need to preserve the formatting of the text, i.e. the HTML tags within the <body> element.

I realise this raises issues of tag closure but there must surely be a way. Hopefully someone more experienced with XSLT can shed some light on this issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:26:13+00:00Added an answer on May 11, 2026 at 7:26 am

    Here’s my version. I’ve tested it over your XML sample and it works.

    To invoke it, use <xsl:apply-templates select='path/to/body/*' mode='truncate'/>.

    <?xml version='1.0' encoding='UTF-8'?> <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>  <xsl:strip-space elements='*'/>  <!-- limit: the truncation limit --> <xsl:variable name='limit' select='250'/>  <!-- t: Total number of characters in the set --> <xsl:variable name='t' select='string-length(normalize-space(//body))'/>  <xsl:template match='*' mode='truncate'>     <xsl:variable name='preceding-strings'>         <xsl:copy-of select='preceding::text()[ancestor::body]'/>     </xsl:variable>      <!-- p: number of characters up to the current node -->     <xsl:variable name='p' select='string-length(normalize-space($preceding-strings))'/>      <xsl:if test='$p &lt; $limit'>         <xsl:element name='{name()}'>             <xsl:apply-templates select='@*' mode='truncate'/>             <xsl:apply-templates mode='truncate'/>         </xsl:element>     </xsl:if> </xsl:template>  <xsl:template match='text()' mode='truncate'>     <xsl:variable name='preceding-strings'>         <xsl:copy-of select='preceding::text()[ancestor::body]'/>     </xsl:variable>      <!-- p: number of characters up to the current node -->     <xsl:variable name='p' select='string-length(normalize-space($preceding-strings))'/>      <!-- c: number of characters including current node -->     <xsl:variable name='c' select='$p + string-length(.)'/>      <xsl:choose>         <xsl:when test='$limit &lt;= $c'>             <xsl:value-of select='substring(., 1, ($limit - $p))'/>             <xsl:text>&#8230;</xsl:text>         </xsl:when>         <xsl:otherwise>             <xsl:value-of select='.'/>         </xsl:otherwise>     </xsl:choose> </xsl:template>  <xsl:template match='@*' mode='truncate'>     <xsl:attribute name='{name(.)}'><xsl:value-of select='.'/></xsl:attribute> </xsl:template>  </xsl:stylesheet> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could try using a code-alongside. By that I mean… May 11, 2026 at 8:45 am
  • added an answer Change this /etc/snmp/snmpd.conf com2sec paranoid default public #com2sec readonly default… May 11, 2026 at 8:45 am
  • added an answer Create a Country object which contains the display name and… May 11, 2026 at 8:45 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.