• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String manipulation

 
Thomas Greene
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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))" />

 
Paul Clapham
Sheriff
Posts: 20982
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic