Meaningless Drivel is fun!
The moose likes XML and Related Technologies and the fly likes String manipulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "String manipulation" Watch "String manipulation" New topic

String manipulation

Thomas Greene
Ranch Hand

Joined: Aug 09, 2004
Posts: 127
I have some image name in my XML in the form of

<image name="abc.jpg">

There is another element


I have to convert this to something like (using XSL)


For this I have written in my XSL

<xsl:value-of select="substring-before(image/@name, '.')" />
<xsl:value-of select="append" />
<xsl:value-of select="substring-after(image/@name, '.')" />

This works perfectly fine. But the problem comes when the name of the image is something like


The correct conversion should be

So that means I need to find the value after the last "."

Please tell me how to do this.
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
This works for extensions having 3 letters ex: doc,jpg etc.

<xsl:value-of select="substring(//image/@name,0,string-length(image/@name)-3)" />
<xsl:value-of select="append" />
<xsl:value-of select="substring(image/@name,string-length(image/@name)-2,string-length(image/@name))" />

For 4 lettered extensions:

<xsl:value-of select="substring(//image/@name,0,string-length(image/@name)-4)" />
<xsl:value-of select="append" />
<xsl:value-of select="substring(image/@name,string-length(image/@name)-3,string-length(image/@name))" />

- Christian
Paul Clapham

Joined: Oct 14, 2005
Posts: 19851

The usual thing to do in XSLT is to write a recursive template when you have to do something repetitive (like searching for the last period in a string). Its logic would be something like this:
I agree. Here's the link:
subject: String manipulation
jQuery in Action, 3rd edition