Page 1 of 1

Test whether file exists

Posted: Fri Mar 24, 2017 11:30 pm
by mboudreau
I'm creating a transformation during which I need to know whether a file exists. I found this solution online:

Code: Select all


    <xsl:choose>
<xsl:when test="fs:exists(fs:new('AJS405045.xml'))" xmlns:fs="java.io.File">
<xsl:message><xsl:text>Found it!</xsl:text></xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:message><xsl:text>Missing!</xsl:text></xsl:message>
</xsl:otherwise>
</xsl:choose>
but after adding this to my stylesheet, the test always fails, but I'm not seeing any error message. (The named file is in the same directory as the stylesheet itself.)

My transformation is running with Saxon-PE 9.6.0.7.

Where do I start to troubleshoot this?

Re: Test whether file exists

Posted: Mon Mar 27, 2017 10:28 am
by Jamil
Your use case is not making sense to me. Based on your description, XSLT does not seem appropriate for what you wish to do. Why not test for file existence in Java instead?

Re: Test whether file exists

Posted: Mon Mar 27, 2017 10:59 am
by adrian
Hi,
the test always fails
So, the transformation is successful but it always goes the xsl:otherwise route. Is that correct?
If you're running this on Linux (also applies to some OS X file systems), the file names are case sensitive, so check the exact file name (uppercase/lowercase letters).
I would also recommend testing with an absolute file path. So, instead of 'AJS405045.xml' use '/path/to/my/file/AJS405045.xml'.

Regards,
Adrian

Re: Test whether file exists

Posted: Thu Mar 30, 2017 10:39 pm
by mboudreau
Sorry for the late reply--I neglected to check "Notify me when a reply is posted".

Thanks, Adrian: specifying the absolute file path solved the problem.

FWIW, I'm running this in Oxygen 18.1 on OS X 10.11.6 (El Capitan), so I'm accustomed to assuming filenames are case-sensitive.

Using the absolute path works with both of the following tests:

Code: Select all

<xsl:when test="file:exists('mrb.txt')" xmlns:file="http://expath.org/ns/file">
and

Code: Select all

<xsl:when test="fs:exists(fs:new('mrb.txt'))" xmlns:fs="java.io.File">