[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: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Wed, 29 Dec 2004 04:48:32 -0800 (PST)
determine file format
> I am not sure what should be done in the case where
> the path doesn't contain a '.' (or how to trap that
>condition) 
I suggest the following XSL to trap this condition..

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

<xsl:template name="TEMP">
   <xsl:param name="x" />

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

</xsl:stylesheet>

With the above XSL, if path contains a . after the
last / character, the stylesheet outputs just as
before (it prints the extension name). But if the path
is something like - <xsl:variable name="lcpath"
select="'/images/dir.path/filepng'" /> , the XSL
prints "No extension".. 

You can choose to write <xsl:text></xsl:text> instead
of <xsl:text>No extension</xsl:text> .. (to output
nothing, if there is no extension)..

>Is there a better way to code the two 
> xsl:when blocks below that basically do the same
> thing but with  different characters?
The way you have modified the code, looks fine to me!

Regards,
Mukul


	
		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail

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.