xmlxsltxslt-2.0

How can I detect a missing file and not throw an error


I mentioned the xml file path in the xsl, but I want to delete that xml file in the folder for now and I always want to keep in the xsl. But when I'm removing it showing the warning.

<xsl:apply-templates mode="replaceVariables"
            select="document('../Folder_1/Test.xml')">
</xsl:apply-templates>

I deleted the Test.xml from the Folder_1. When I'm running I'm getting the below error

Engine name: Saxon-PE 9.9.1.7
Severity: warning
Description: I/O error reported by XML parser processing file:/C:/Work/Folder_1/Test.xml: C:/Work/Folder_1/Test.xml (The system cannot find the file specified)
Start location: 674:48
Length: 1

Even though the xml file delete in the corresponding mentioned folder it shouldn't show error while converting.


Solution

  • In XSLT 2.0 and later there is the XPath 2.0 and later function doc-available e.g. doc-available('../Folder_1/Test.xml') so you can use an xsl:if check around your xsl:apply-templates or you might even be able to use the check in a predicate of the select expression.