Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XSLT to strip file extension, perhaps with regex?

Posted on 2014-12-19
5
Medium Priority
?
450 Views
Last Modified: 2014-12-19
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
Comment
Question by:GessWurker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40509345
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
 

Author Comment

by:GessWurker
ID: 40509399
Yep. Using Saxon XSLT 2.0 processor
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 40509573
<xsl:value-of select="replace( ., '\.[^\.]+$', '')"/>

make sure you set the xslt version to 2.0
0
 

Author Comment

by:GessWurker
ID: 40509601
Thanks, Geert. Your suggestion is sufficiently robust for my application. Now I'll post a separate (but related) question.
0
 

Author Comment

by:GessWurker
ID: 40509603
Ah... I missed the regex part. Even better! And now I'll post another related question...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question