Page 1 of 1

XSL - testing if a document is valid

Posted: Tue Sep 20, 2016 7:13 am
by rdelong
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:choose>
<xsl:when test="contains(@filetype, 'ISHIllustration')">
<xsl:attribute name="doctype">illustration</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="filePathFull">
<xsl:text>file:/</xsl:text><xsl:value-of select="$basePath"/>/<xsl:value-of select="$filePath"/>
</xsl:variable>
<xsl:attribute name="doctype" select="document($filePathFull)/name(/*)"/>
</xsl:otherwise>
</xsl:choose>

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".

Thanks

Re: XSL - testing if a document is valid

Posted: Tue Sep 20, 2016 9:46 am
by Radu
Hi,

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

Regards,
Radu