How do I use substring in xsl?

Posted on 2013-01-15
Last Modified: 2013-01-18

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?

Question by:aspnet-scotland
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
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?
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

Author Comment

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?

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

LVL 60

Accepted Solution

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

Author Comment

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.

LVL 60

Expert Comment

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

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RSS is valid, but improvement recommendations are confusing. 3 62
XML SQL 8 32
How does proportional-column-width work in xsl fo 4 73
Powershell Regex Replace Question 5 43
Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.

751 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