RE: [xsl] Get file name without path info

Subject: RE: [xsl] Get file name without path info
From: "Diamond, Jason" <Jason.Diamond@xxxxxxx>
Date: Fri, 8 Mar 2002 13:06:30 -0600
Unfortunately, there's no substring-after-last function so you have to write
a recursive named template to strip off all the path segments until there's
no more slashes in your string.

If you're sure that the filename will only contain one dot then the
following will work:

<xsl:transform version='1.0'
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
>
	<xsl:output method='text' encoding='utf-8' />

	<xsl:template match='/'>
		<xsl:variable name='file'>
			<xsl:call-template name='strip-path'>
				<xsl:with-param name='path'
select='"file:/C:/Documents and Settings/nshaw.HQIRMS/My
Documents/spaceresearch/newxml/general_info/what.xml"' />
			</xsl:call-template>
		</xsl:variable>
		<xsl:value-of select='concat(substring-before($file, "."),
"_p.html")' />
	</xsl:template>

	<xsl:template name='strip-path'>
		<xsl:param name='path' />
		<xsl:choose>
			<xsl:when test='contains($path, "/")'>
				<xsl:call-template name='strip-path'>
					<xsl:with-param name='path'
select='substring-after($path, "/")' />
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select='$path' />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:transform>

Or, you can use the substring-after-last template form the XSLTSL
(http://xsltsl.sourceforge.net/).

Hope this helps,
Jason

> -----Original Message-----
> From: Nathan Shaw [mailto:n8_shaw@xxxxxxxxx]
> Sent: Friday, March 08, 2002 10:48 AM
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Get file name without path info
> 
> 
> I am using saxon:system-id() to get the name of the
> xml file being parsed. However, as you probably know,
> Saxon returns the entire path along with the file
> name. I want to just get the file name, strip off the
> '.xml' and replace it with '_p.html'.
> 
> So, Saxon gives me this:
> file:/C:/Documents and Settings/nshaw.HQIRMS/My
> Documents/spaceresearch/newxml/general_info/what.xml
> 
> and I want to end up with this:
> what_p.html
> 
> I am feeling rather handicapped by XSL when it comes
> to doing this. I am used to having functions like
> split(), gettoken(), replace(), etc... available to
> me.
> 
> Can someone give me some guidance as to how to do this
> in XSL?
> 
> Thanks,
> 
> --Nate
> 
> __________________________________________________
> Do You Yahoo!?
> Try FREE Yahoo! Mail - the world's greatest free email!
> http://mail.yahoo.com/
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread