• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String manipulation

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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))" />

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic