Project

Profile

Help

Saxon.Api.DynamicError running XsltTransformer

Added by Marco Serioli over 6 years ago

Dear all,

I'm using the saxon9he-api 9.8.0.11 into a .net C# project. I apply an XSLT to incoming xml files. Sometimes the files are not "compliant" with the XSLT and thus the method

public void Run(XmlDestination destination);

throws the exception Saxon.Api.DynamicError. My aim is to provide some meaningful instruction to the user when such errors occurs. So far the only error I can get is the error relate to the applied xslt. Eg:

Error at char 40 in xsl:sequence/@select on line 4708 column 94 of file.xslt: FORG0001: Cannot convert string "" to xs:decimal: no digits in value Exception thrown: 'Saxon.Api.DynamicError' in saxon9he-api.dll An unhandled exception of type 'Saxon.Api.DynamicError' occurred in saxon9he-api.dll

I would prefer to provide the user with information about where the error is in the xml file. Is there a way to do this?

Thanks.


Replies (1)

RE: Saxon.Api.DynamicError running XsltTransformer - Added by Michael Kay over 6 years ago

The best approach is probably to use a schema-aware transformation: validate the source document against a schema before it gets processed by the XSLT.

Another approach is to put extra logic in the stylesheet: before you try to convert a string to xs:decimal (typically with the constructor function xs:decimal(expr)), insert a test that the value is actually castable:


  Value at line {saxon:line-number(XXX))} is not a valid xs:decimal value

making sure that line-numbering of source documents is enabled, e.g. by setting the appropriate property on the DocumentBuilder.

A third approach which may sometimes work is to look at the Context property of the XPathException within the thrown exception. If this is present you can call getContextItem() to find out what was the context item at the point of failure, and if this is a node, you can call getLineNumber() to find its line number. However, this will give a "wrong" answer if the value you were converting wasn't actually the context item.

    (1-1/1)

    Please register to reply