XSL - testing if a document is valid

Having trouble installing <oXygen/>? Got a bug to report? Post it all here.
Posts: 71
Joined: Tue Oct 21, 2014 10:01 pm

XSL - testing if a document is valid

Post by rdelong » Tue Sep 20, 2016 7:13 am

I'm processing a number of files using an XSL document and I've discovered that a few of the files are not valid. When open them (using the document() function) I get the following error message and transform stops:

Code: Select all

Severity: fatal
Description: Premature end of file.
URL: http://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/trans/SaxonErrorCode.html#SXXP0003
Here's a snippet of the xslt code that I'm working with:

Code: Select all

<xsl:when test="contains(@filetype, 'ISHIllustration')">
<xsl:attribute name="doctype">illustration</xsl:attribute>
<xsl:variable name="filePathFull">
<xsl:text>file:/</xsl:text><xsl:value-of select="$basePath"/>/<xsl:value-of select="$filePath"/>
<xsl:attribute name="doctype" select="document($filePathFull)/name(/*)"/>

Is there a way to test for a valid XML file and continue if OK, but skip the file if it's not valid?
Specifically, I'd have the transform to use the valid value for @doctype, but when an error is found use @doctype="unknown".


Posts: 7102
Joined: Fri Jul 09, 2004 5:18 pm

Re: XSL - testing if a document is valid

Post by Radu » Tue Sep 20, 2016 9:46 am


Maybe you should try to use the doc-available function, it should return "false" if the XML is not wellformed:

https://www.w3.org/TR/xpath-functions/# ... -available

Radu Coravu
<oXygen/> XML Editor

Post Reply