5

I would like to check in XSLT whether an HTML file exists or not. How can I do this? I have already tried the file-exists.xsl from here https://gist.github.com/emth/4531924 but it doesn't work for me. I've been trying to get it running for over 2 hours now, but I am stuck. Here's my ant snippet:

<target name="transform">
    <xslt in="/tmp/sample.xml" out="/tmp/out.html" style="/tmp/sample.xsl" />
</target>

and this is my xslt file:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:java="http://www.java.com/">
<xsl:import href="file-exists.xsl"/>
...    
<xsl:if test="java:file-exists('myfile.html', base-uri())">
    <!-- do something here... -->
</xsl:if>
....

Running this with ant I will get the following error:

[xslt] Processing /tmp/sample.xml to /tmp/out.html
[xslt] Loading stylesheet /tmp/sample.xsl
[xslt] : Error! The first argument to the non-static Java function 'fileExists' is not a valid object reference.
[xslt] : Error! Cannot convert data-type 'void' to 'boolean'.
[xslt] : Fatal Error! Could not compile stylesheet
[xslt] Failed to process /tmp/sample.xml

Can anybody provide me a running example or is there any other alternative? Thanks!

user1613270
  • 523
  • 10
  • 20
  • I think the library file-exists.xsl use XSL engin 2.0 but you run it in version 1.0 http://stackoverflow.com/questions/2917655/how-do-i-check-for-the-existence-of-an-external-file-with-xsl – Sawyer Nov 27 '13 at 13:18
  • It doesn't matter whether I use version 1.0 or 2.0. I get the same error with 2.0 – user1613270 Nov 27 '13 at 13:37

2 Answers2

6

I've found a solution:

<xsl:when test="fs:exists(fs:new('myfile.html'))" xmlns:fs="java.io.File">
    <!-- do something here... -->
</xsl:when>

and it works independently of XSLT 1.0 or 2.0

user1613270
  • 523
  • 10
  • 20
3

If the file is a text file, you can use the XSLT 2.0 function:

fn:func-unparsed-text-available()

The fn:unparsed-text-available function determines whether a call on the fn:unparsed-text function with identical arguments would return a string. The function will attempt to read the resource identified by the URI, and check that it is correctly encoded and contains no invalid characters.

This is similar to fn:doc-available() which works on XML documents instead.

Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65
  • It is actually an HTML document that I need to check the existence – user1613270 Nov 27 '13 at 14:05
  • Yep. That will work. So use `fn:func-unparsed-text-available()`. – Sean B. Durkin Nov 27 '13 at 14:17
  • Unfortunately it doesn't work for me? I get errors: Error! Error checking type of the expression 'funcall(func-unparsed-text-available, [literal-expr(myfile.html)])'. Fatal Error! Could not compile stylesheet – user1613270 Nov 27 '13 at 14:25
  • If you are using a compliant xslt 2.0 processor, it has to work. This is a standard function. Please show your stylesheet with use of fn:func-unparsed-text-available(). Also what is your XSLT processor? – Sean B. Durkin Nov 27 '13 at 14:31
  • I am not sure which XSLT processor is used, it's the one which comes with Ant 1.8.2 – user1613270 Nov 27 '13 at 14:42
  • Use system-property('xsl:vendor') and system-property('xsl:vendor-url') and system-property('xsl:version') to find out. – Sean B. Durkin Nov 27 '13 at 14:48
  • This might be helpful if it turns out it's not XSLT 2.0: http://stackoverflow.com/questions/919692/how-to-execute-xslt-2-0-with-ant – Flynn1179 Nov 28 '13 at 10:30