Jump to content

Extract filenames from different pathes


Loriot

Recommended Posts

Hi helpful people out there. I am trying to extract the filname out of different pathes. As I don't know the length an number of slashes in the path, I tried to turn it round, take the backwards part until the first slash and turn the result one more time, but it doesn't work like that:

<xsl:value-of select="reverse(substring-before(reverse(File_Filename0),'/'))"/>

Maybe someone here has some better idea how I solve that???RegardsLoriot

Link to comment
Share on other sites

The first thing I think I need to know is if you use Saxon or any other (which one btw?) XSLT 2.0 processor? If not, the reverse() function won't work, scince it's XPath 2.0 function.If you are using an XSLT 2.0 processor, I think that the reverse() function reverses only the order of existing items in a node set. As in <xsl:sort order="ascending"/>, but within the seqence itself. Because of that, I think this would return an error on strings, though I'm not sure.How exactly does your input look like btw? If you're trying to extract only the filename in a certain URL string and you ARE using an XSLT 2.0 processor, then perhaps something like the tokenize() function might help.

<xsl:value-of select="tokenize(File_Filename0,'\/')[position()=last()]"/>

Link to comment
Share on other sites

Hi helpful,unfortunately I came to the same point as you, taht I can't use the reverse function (no 2.x parser...). My filenames look like that:\\someplace09\ak01\AK_RASTOFF\KJHGF46927_90D8BAE4D3124E0383EC647A13403DCE.WAV or like that\\someplace15\MediaDir\somename\20061106\ABCDEF46927_80C49B8BC2D345DCB6F48F32E4D3B8AB_1H.WAVand I need to extract just the filenames:KJHGF46927_90D8BAE4D3124E0383EC647A13403DCE.WAVABCDEF46927_80C49B8BC2D345DCB6F48F32E4D3B8AB_1H.WAVI go checking out the tokenize function. Never tried that before. Maybe it helps. Thanks for this first hint.Loriot....tokenize doesn't work out for me. Could you probably give me an example?<xsl:value-of select="tokenize(File_Filename0,'\/')/> leads to an error message that tokenize is an unknown function?

Link to comment
Share on other sites

If you're trying to extract only the filename in a certain URL string and you ARE using an XSLT 2.0 processor
As you said, you don't have an XSLT 2.0 processor, so the error is to be expected.Honestly said, I can't think of a way to do this in XSLT 1.0. It will requre much string manipulation, and XPath 1.0 capabilities are not that strong.What processor exactly are you using anyway? libxslt? Xalan (god knows the host language)? If you're using libxslt (say on PHP5) you can use the EXSLT tokenize() function in the very same way as desribed above.
Link to comment
Share on other sites

Hi Boen, I am using som special prosessor implementet in a microsoft based professional radio software. You won't know it. So it seems that I fail this time. Thanks anyway so far.I was thinking round my problem.Functions that could help me were-extract some substring between signsor-turn round string and substring beforor-take substring after last slash (or whatever I translate the slashes to)or-devide value in several values at slashes, then I could continue with last value afterwardsor?I am clueless on all this. But maybe one has an idea?? Maybe there is some java solution I havn't thought of, because I am no java prof.Any hint is appriciated!Loriot :)

Link to comment
Share on other sites

-take substring after last slash (or whatever I translate the slashes to)
Translated or not, there's no way to get a substring-after() on last match. The function itself returns the string after the first occurance of the second argument, not it's last. Using several substring-after() would do, but you'll have to know the depth of the file in advance.
-devide value in several values at slashes, then I could continue with last value afterwards
This is actually what the tokenize() function does. It devices the string into nodes that you can then access with an XPath expression, so that you can only get the last one.
Link to comment
Share on other sites

Using recursion, this can be done with 1.1 XSLTThis XML

<files>	<file>\\someplace09\ak01\AK_RASTOFF\KJHGF46927_90D8BAE4D3124E0383EC647A13403DCE.WAV</file>	<file>\\someplace15\MediaDir\somename\20061106\ABCDEF46927_80C49B8BC2D345DCB6F48F32E4D3B8AB_1H.WAV</file></files>

This XSLT

	<xsl:template match="/">		<xsl:for-each select="//file">			<xsl:call-template name="GetFileName" /><hr/>		</xsl:for-each>	</xsl:template>	<xsl:template name="GetFileName">		<xsl:param name="value" select="text()" />		<xsl:choose>			<xsl:when test="contains($value,'\')">				<xsl:call-template name="GetFileName">					<xsl:with-param name="value" select="substring-after($value,'\')"/>				</xsl:call-template>			</xsl:when>			<xsl:otherwise>				<xsl:value-of select="$value"/>			</xsl:otherwise>		</xsl:choose>	</xsl:template>

This is the result

KJHGF46927_90D8BAE4D3124E0383EC647A13403DCE.WAV--------------------------------------------------------------------------------ABCDEF46927_80C49B8BC2D345DCB6F48F32E4D3B8AB_1H.WAV--------------------------------------------------------------------------------

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...