XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 10th, 2004, 03:20 AM
mattisimo mattisimo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 5 mattisimo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
XSL String Manipulation

Hi,
I am trying to find the file extension from a string.
e.g. "test.doc" or "test.html" but the string is going to have several . in it as it will be from a URL. Is there a simple/clean way of separating the sting and extracting the file extension?
I was thinking of splitting the string by instance of . and then selecting the last section.
Any ideas on this?
Thanks for the help,
Matt

Reply With Quote
  #2  
Old February 10th, 2004, 01:45 PM
MattSidesinger's Avatar
MattSidesinger MattSidesinger is offline
Java PHP Oracle Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: C-Bus OH-IO
Posts: 204 MattSidesinger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 26 sec
Reputation Power: 5
Send a message via AIM to MattSidesinger
Let me know if you need any explaining.

Code:
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- this variable must be a length of 1 -->
<xsl:variable name="DELIMETER_FILE_EXTENSION" select="'.'"/>

<xsl:template match="/">
	
	<xsl:variable name="file" select="'this.is.a.file.txt'"/>

	<!-- example: print out the file extension -->
	<xsl:call-template name="getFileExtension">
		<xsl:with-param name="file" select="$file"/>
	</xsl:call-template>

	<!-- example: store the file extension in a variable -->
	<xsl:variable name="fileExtension">
		<xsl:call-template name="getFileExtension">
			<xsl:with-param name="file" select="$file"/>
		</xsl:call-template>
	</xsl:variable>
	
</xsl:template>

<xsl:template name="getFileExtension">
	<xsl:param name="file"/>
	
	<xsl:if test="contains($file, $DELIMETER_FILE_EXTENSION)">
	
		<xsl:variable name="fileLength" select="string-length($file)"/>
		
		<xsl:variable name="delemiterPosition">
			<xsl:call-template name="getDelemiterPosition">
				<xsl:with-param name="file" select="$file"/>
				<xsl:with-param name="startSearchPosition"
						select="$fileLength"/>
			</xsl:call-template>
		</xsl:variable>
		
		<xsl:value-of select="substring($file,
						$delemiterPosition + 1,
						$fileLength - $delemiterPosition + 1)"/>
		
	</xsl:if>
	
</xsl:template>

<xsl:template name="getDelemiterPosition">
	<xsl:param name="file"/>
	<xsl:param name="startSearchPosition"/>
	
	<xsl:if test="$startSearchPosition > 0">
		<xsl:choose>
			<xsl:when test="substring($file, $startSearchPosition, 1) = $DELIMETER_FILE_EXTENSION">
				<xsl:value-of select="$startSearchPosition"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="getDelemiterPosition">
					<xsl:with-param name="file" select="$file"/>
					<xsl:with-param name="startSearchPosition"
							select="$startSearchPosition - 1"/>
				</xsl:call-template>		
			</xsl:otherwise>
		</xsl:choose>
	</xsl:if>
	
</xsl:template>

</xsl:stylesheet>

Last edited by MattSidesinger : February 10th, 2004 at 01:49 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL String Manipulation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT