Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

XSLT to strip file extension, perhaps with regex?

I use the transform below to remove the '.pdf' file extension from processed files. I'd like to come up with a transform that will remove file extensions regardless of file type, e.g., .doc, .docx, .txt, .ppt, zip., .jpeg, etc etc etc    Can this be done via regex, perhaps? I don't know how to do this. Can someone show me how to adjust the code to deal with any file extension?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:idoc="http://ns.inmagic.com/Presto/1.0/ContentConnector/DocumentParameters">
    <xsl:output omit-xml-declaration="no" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
   
    <xsl:template match="idoc:ItemName[normalize-space(.)]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:value-of select="replace( ., '.pdf', '' )"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Thanks.
0
GessWurker
Asked:
GessWurker
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
Note that the translate() is not taking away the ".pdf" but is taking away the '.' the 'p' the 'd' and the 'f'

so the filename 'pdf.pdf' will be erased completely

replace() and regex are the best answer, but you have a XSLT1 and those are XSLT2 functionality.
What is your XSLT processor? Can you use XSLT2?

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

is the easiest cut, but it will only work correctly if there is only one '.' in the file name
it will return 'foo' if the filename is 'foo.bar.pdf'

if you need a more solid solution, you will need recursion
but first tell me if you can use XSLT2
0
 
GessWurkerAuthor Commented:
Yep. Using Saxon XSLT 2.0 processor
0
 
Geert BormansCommented:
<xsl:value-of select="replace( ., '\.[^\.]+$', '')"/>

make sure you set the xslt version to 2.0
0
 
GessWurkerAuthor Commented:
Thanks, Geert. Your suggestion is sufficiently robust for my application. Now I'll post a separate (but related) question.
0
 
GessWurkerAuthor Commented:
Ah... I missed the regex part. Even better! And now I'll post another related question...
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now