[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Determine file extension of path stored in variabl

Subject: Re: Determine file extension of path stored in variable
From: John <john-xsl-list@xxxxxxxx>
Date: Tue, 28 Dec 2004 09:15:23 -0800
java get file extension
Thanks. I had a feeling the alternative was recursion, and I think that's better if I use this function a lot, but it actually seems like
more code that is harder to follow (especially since I have to handle the case where file name contains a '.' before the '.' that may come before the extension). In any case your approach is the one I will take as it is more general.


You were right, the missing '' came from me trying to simplify the code for post (the variable is not hard-coded in the real code). Sorry about that.

I am not sure what should be done in the case where the path doesn't contain a '.' (or how to trap that condition) - in this case I guess it will return the full path. Is there a better way to code the two xsl:when blocks below that basically do the same thing but with different characters?

<xsl:template name="get-file-extension">
<xsl:param name="path" />
<xsl:choose>
<xsl:when test="contains( $path, '/' )">
<xsl:call-template name="get-file-extension">
<xsl:with-param name="path" select="substring-after( $path, '/' )" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains( $path, '.' )">
<xsl:call-template name="get-file-extension">
<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>


Mukul Gandhi wrote:
You can also try a recursive approach..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />


<xsl:variable name="lcpath"
select="'/images/dir.path/file.png'" />

<xsl:template match="/">
  <xsl:variable name="ext">
    <xsl:call-template name="get-file-extension">
      <xsl:with-param name="path" select="$lcpath" />
    </xsl:call-template>
  </xsl:variable>
  <xsl:value-of select="$ext" />
</xsl:template>

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


</xsl:stylesheet>
Please note the use of ' in <xsl:variable
name="lcpath".. This way, contents of variable will be
treated as literal string. The way you have used it.. <xsl:variable name="lcpath"
select="/images/dir.path/file.png" /> , the variable
is bound to a XPath expression.. I hope, you mean
literal string..


You may test the above XSL as -
java net.sf.saxon.Transform file.xsl file.xsl

Regards,
Mukul

--- John <john-xsl-list@xxxxxxxx> wrote:


What is the best way to determine the extension of a
file when the webroot-relative path is stored in a variable,
assuming the path may contain any number of "." characters (so I can't use
substring-after)? For instance if $lcpath contains
/images/dir.path/file.png how do I set $ext to be "png"? Below is the best I have been
able to come up with - suggestions greatly appreciated.


<xsl:variable name="lcpath"
select="/images/dir.path/file.png" />

<xsl:variable name="pos">
<xsl:choose>
<xsl:when test="substring( $lcpath,
string-length( $lcpath ) - 5, 1 ) = '.'">4</xsl:when>
<xsl:when test="substring( $lcpath,
string-length( $lcpath ) - 4, 1 ) = '.'">3</xsl:when>
<xsl:when test="substring( $lcpath,
string-length( $lcpath ) - 3, 1 ) = '.'">2</xsl:when>
<xsl:when test="substring( $lcpath,
string-length( $lcpath ) - 2, 1 ) = '.'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="ext" select="substring( $lcpath,
string-length( $lcpath ) - $pos )" />

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.