Project

Profile

Help

Saxon XSLT error pos always returns Column#-1

Added by Anonymous over 18 years ago

Legacy ID: #3882364 Legacy Poster: Helmoe (helmoe)

I am using the SaxonB 8.7.3 to parse a XSL file. It works great - except when there is an error in the xsl file. Saxon doesn't allways return the column numer where the error is (the line number is ok)??? Can someone please explain to me what i am doing wrong? Here is an example (in java): public class Main { private static String xslt = "<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>\n" + "<xsl:stylesheet version=&quot;2.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform\&quot;&gt;\n" + "<xsl:output encoding=&quot;UTF-8&quot; indent=&quot;no&quot; method=&quot;text&quot; omit-xml-declaration=&quot;yes&quot;/>\n" + "<xsl:param name=&quot;wrap&quot;/>\n" + "<xsl:template match=&quot;indexbio&quot;>\n" + "<xsl:text>&lt;i-term&gt;(</xsl:text>\n" + "<xsl:value-of select=&quot;translate(//i-term/text(),' ','|')&quot;/>\n" + "<xsl:text>)</xsl:text><xsl:choose></xsl:choose>\n" + "</xsl:template>\n" + "</xsl:stylesheet>\n"; public static void main(String[] args) { javax.xml.transform.stream.StreamSource streamSource = new javax.xml.transform.stream.StreamSource(new java.io.StringReader(xslt)); net.sf.saxon.Configuration config = new net.sf.saxon.Configuration(); config.setErrorListener(new javax.xml.transform.ErrorListener() { public void warning(javax.xml.transform.TransformerException exception) throws javax.xml.transform.TransformerException { System.out.println(exception.getMessageAndLocation() + "\n"); } public void error(javax.xml.transform.TransformerException exception) throws javax.xml.transform.TransformerException { System.out.println(exception.getMessageAndLocation() + "\n"); } public void fatalError(javax.xml.transform.TransformerException exception) throws javax.xml.transform.TransformerException { System.out.println(exception.getMessageAndLocation() + "\n"); throw exception; } }); net.sf.saxon.TransformerFactoryImpl factory = new net.sf.saxon.TransformerFactoryImpl(config); try { net.sf.saxon.PreparedStylesheet styleSheet = (net.sf.saxon.PreparedStylesheet)factory.newTemplates(streamSource); } catch (Exception e) { System.out.println("E = " + e); } } }


Replies (4)

Please register to reply

RE: Saxon XSLT error pos always returns Column#-1 - Added by Anonymous over 18 years ago

Legacy ID: #3882368 Legacy Poster: Helmoe (helmoe)

I forgot to add that the above example returns: xsl:choose must contain at least one xsl:when; SystemID: ; Line#: 8; Column#: -1 E = javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected.

RE: Saxon XSLT error pos always returns Colum - Added by Anonymous over 18 years ago

Legacy ID: #3882403 Legacy Poster: Michael Kay (mhkay)

Saxon doesn't generally maintain column numbers; the JAXP interface spec says that getColumnNumber() should return -1 if no column number is available. The main reason for this is that the SAX specification doesn't deliver location information with enough granularity. According to SAX, the XML parser reports the line and column number of the end of the start tag for an element. Usually the error is in the element name, or in the value of one of its attributes, and reporting the column number where the start tag ends is misleading. Somehow if you report an error at line 26 column 1, people are happy to read forwards to find the error in column 50, but if you report the error at column 50 then they aren't inclined to read backwards to column 1. This problem also affects line numbering of course, since if a start tag occupies several lines (increasingly likely with large numbers of namespaces and long XPath expressions), then the line number will also reflect the end of the start tag rather than the location of the actual attribute.

RE: Saxon XSLT error pos always returns Column#-1 - Added by Anonymous over 18 years ago

Legacy ID: #3882517 Legacy Poster: Helmoe (helmoe)

Ok - thank you for your quick reply.

RE: Saxon XSLT error pos always returns Column#-1 - Added by Anonymous over 18 years ago

Legacy ID: #3882614 Legacy Poster: Helmoe (helmoe)

After thinking about your answer - I don't quite agree. If I insert newlines (off cource the parser has to not care about whitespace) after every tag i will get a more precise error position - namely the line the error is in. This compared to a one-liner with all the text is a huge difference. If I in the example above removed all "\n" then the result would be: xsl:choose must contain at least one xsl:when; SystemID: ; Line#: 1; Column#: -1 E = javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected. With regards to SAX only returning the end position of the start tag - then you only have to remember last valid position and then return this - and then let the reader read forwards from this position..! I might have misunderstood something - but this is just what my intuition tells me ;-)

    (1-4/4)

    Please register to reply