GeeCON Prague 2014*
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


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "String manipulation" Watch "String manipulation" New topic
Author

String manipulation

Thomas Greene
Ranch Hand

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

<image name="abc.jpg">

There is another element

<append>xyz</append>

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

abc_xyz.jpg

For this I have written in my XSL

<xsl:value-of select="substring-before(image/@name, '.')" />
<xsl:text>_</xsl:text>
<xsl:value-of select="append" />
<xsl:text>.</xsl:text>
<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

abc.123.jpeg

The correct conversion should be
abc.123_xyz.jpeg

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
Hi,
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:text>_</xsl:text>
<xsl:value-of select="append" />
<xsl:text>.</xsl:text>
<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:text>_</xsl:text>
<xsl:value-of select="append" />
<xsl:text>.</xsl:text>
<xsl:value-of select="substring(image/@name,string-length(image/@name)-3,string-length(image/@name))" />



- Christian
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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:
 
GeeCON Prague 2014
 
subject: String manipulation