Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check whether a file exists with XSLT2 (a Java extension -powered function)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:java="http://www.java.com/"
exclude-result-prefixes="java xs">
<xsl:function name="java:file-exists" xmlns:file="java.io.File" as="xs:boolean">
<xsl:param name="file" as="xs:string"/>
<xsl:param name="base-uri" as="xs:string"/>
<xsl:variable name="absolute-uri" select="resolve-uri($file, $base-uri)" as="xs:anyURI"/>
<xsl:sequence select="file:exists(file:new($absolute-uri))"/>
</xsl:function>
</xsl:stylesheet>
@eerohele

This comment has been minimized.

Copy link
Owner Author

commented Jan 14, 2013

Use like this:

<xsl:if test="java:file-exists($filename, base-uri())">
  <!-- ... -->
</xsl:if>
@holmbergius

This comment has been minimized.

Copy link

commented Jun 21, 2013

Works beautifully. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.