xsl-list
[Top] [All Lists]

Re: [xsl] another beginner question - strip directory info from variable

2018-10-14 01:00:38
Hi Dave,

The way I solve this problem is using replace(string, regex,
replacement). Regular expression syntax can be daunting, but they are
very powerful. Blow are a couple of functions that I use.

Good luck,

Pieter

    <!-- The filename part of a pathname, including the extension. -->
    <xsl:function name="ivdnt:get-base-filename" as="xs:string">
        <xsl:param name="path" as="xs:string"/>
        <xsl:value-of select="replace($path, '^(.*/)?([^/]+)/?$','$2')"/>
    </xsl:function>
   
    <!-- The directory part of a pathname. -->
    <xsl:function name="ivdnt:get-directory-name" as="xs:string">
        <xsl:param name="path" as="xs:string"/>
        <xsl:variable name="dir" as="xs:string" select="replace($path,
'^(.*/)?([^/]+)/?$','$1')"/>
        <xsl:value-of select="if ($dir eq '') then '.' else $dir"/>
    </xsl:function>
   
    <!-- Return the pathname without the filename extension (also
excluding the "."). -->
    <xsl:function name="ivdnt:strip-filename-extension" as="xs:string">
        <xsl:param name="path" as="xs:string"/>
        <xsl:value-of select="replace($path,
'^((.*/)?([^/]+))\.[^./]+$', '$1')"/>
    </xsl:function>
   
    <!-- Returns the extension of the pathname, without the ".". -->
    <xsl:function name="ivdnt:get-filename-extension" as="xs:string">
        <xsl:param name="path" as="xs:string"/>
        <!-- The extra condition with matches() is needed because
otherwise a filename without dot will lead to the filename
             being returned instead of an empty string.
        -->
        <xsl:value-of select="if (matches($path, '^.*\.[^./]+$')) then
replace($path, '^.*\.([^./]+)$', '$1') else ''"/>
    </xsl:function>

On 10/14/2018 03:49 AM, Dave Lang emaildavelang(_at_)gmail(_dot_)com wrote:
Hi again everyone - back with another beginner question.

I'm working in Oxygen and am using Saxon.

I'm searching through a bunch of xml files in search of jpg names. I'm
creating variables as follows:

<xsl:variable name="allxml"
select="collection('..//xml/?select=*.xml;recurse=yes')"/>
<xsl:variable name="jpgs" select="$allxml//element/@n"/>

$jpgs looks like:

dir1/jpg001.jpg dir1/jpg002.jpg dir1/jpg003.jpg

etc.

I want to strip away the dir info, but am having a hard time. I've
tried using tokenize and substring-after but both return similar errors.

"A sequence of more than one item is not allowed as the first argument
of fn:tokenize() ("dir1/jpg001.jpg", "dir1/jpg002.jpg")."

Because I don't know what I'm doing, I don't know what I'm doing
wrong. :)

Can anyone help me figure out how to solve this problem?

thanks!
dave


--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

Attachment: signature.asc
Description: OpenPGP digital signature

<Prev in Thread] Current Thread [Next in Thread>