Solved

How do I use substring in xsl?

Posted on 2013-01-15
6
212 Views
Last Modified: 2013-01-18
Hi,

I have the below xsl code:

<xsl:value-of select="substring(@Description,1,250)" disable-output-escaping="yes" />
<xsl:if test="string-length(@Description) &gt; 250">...</xsl:if>

Open in new window


This displays the first 250 characters of the description, and "..." if there are more characters in the description than being displayed. How can I alter this so only 50 characters are displayed? When I changed both instances of "250" to "50" no text was shown?

Thanks.
0
Comment
Question by:aspnet-scotland
  • 4
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38780444
well, did you not change anything else?

changing 250 to 50 in both instances is the way to go

maybe you only have spaces in the first 50 chars?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38780456
maybe you need to tell us what happens next to the result of this XSLT
Maybe you have a process in place that removes a text if not long enough

Bottomline as an answer to your question is
250 to 50 should work in this isolated case
but check what happens next
0
 

Author Comment

by:aspnet-scotland
ID: 38780512
Strange....when I reduced it to "100" only around 20 characters were shown, I guess there must be white space hidden somewhere as firebug doesn't show anything?

Thanks.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 38780545
Well, if it is bigger than 50, you should see the "..."

A browser does away with spurious white-space
A good test could be

<xsl:variable name="norm-desc" select="translate(normalize-space(@Description), ' ', '_')"/>
<xsl:value-of select="substring($norm-desc,1,250)" disable-output-escaping="yes" />
<xsl:if test="string-length($norm-desc) &gt; 250">...</xsl:if>

Open in new window


This takes out all sequences of whitespace in favour of a single underscore
(if you leave out the normalize-space, you will see all spaces as underscore)

It will help you to guess the number of whitespace in your input
0
 

Author Comment

by:aspnet-scotland
ID: 38782064
Thanks for your code. I can see that all whitespace have now been replaced with an underscore but no rogue whitespace seems to be present. I seem to have lost around 50 characters somewhere. Strange.

Thanks.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38782077
Maybe if you could post the source XML and the full stylesheet, that could help
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
installing and using WTP plugin eclipse MARS 3 124
XSLT Help 12 34
Create XML 5 52
Xsl-fo show PDF when no results are returned 2 20
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question