简体   繁体   中英

How do I pass an error message generated by xslt to java application caller

I use a java application to call a xslt to do a xml transformation. The xslt file will generate a message and terminate the process if some condition happens. However, my java application couldn't catch the error message generated by xslt, it only catch an exception with general information - "Stylesheet directed termination".

Here is my java code:

SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
    // Create a TransformerHandler for  stylesheet.
    File f2 = new File(styleSheetPath);

TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(f2));

XMLReader reader = XMLReaderFactory.createXMLReader();

reader.setContentHandler(tHandler2);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler2);

    CharArrayWriter outputWriter  = new CharArrayWriter();
    Result result = new StreamResult(outputWriter);
    tHandler2.setResult(result);

    try
    {
      reader.parse(new InputSource(new StringReader(XMLinput)));
    }
    catch(Exception ee)
    {
        dsiplay(ee.getMessage())
        throw ee;
    }

How can I catch the error message from xslt?

I tried to write a class:

private class MyErrorHandler extends DefaultHandler{

  public void error(SAXParseException e)
  {
      System.out.println("error method "+e.getMessage());
  }

  public void fatalError(SAXParseException e)
  {
      System.out.println("fatal error method "+e.getMessage());
  }

  public void warning(SAXParseException e)
  {
      System.out.println("warning method "+e.getMessage());
  }

and

MyErrorHandler myHandler = new MyErrorHandler();

reader.setErrorHandler(myHandler);

It didn't work.

Do you have any suggestion?

It looks as if you have placed an error handler on the source document XML parser, but are trying to catch errors from the transformer. So place the error handler on the Transformer rather than the XMLReader

tHandler2.getTransformer().setErrorListener(myHandler);

IIRC, I've always kept it simple and used Transformer directly rather than go for ContentHandler / TranformerHandler .

As alternatives you could go for an implementation specific extension or cause a special URI to be resolved and handle that (although as XSLT is a functional language, I guess it could technically resolve a URI that wont actually impact the result).

In what way is the XSLT generating an error and terminating? This might be crucial since the <xsl:message> tag does not always write to the standard output like you might expect. Especially with Saxon.

Thank you so much for the replying. Here is my xslt code:

<xsl:template match="*[not(*) and ((text() and not(normalize-space(text()) != '')) or .='') 
       and name(.) != 'para' and name(.) != 'recordDelimiter' and name(.) != 'physicalLineDelimiter' and name(.) != 'fieldDelimiter' ]">
      <xsl:message terminate="yes">
        <xsl:call-template name="output_message3_fail">
          <xsl:with-param name="current_node" select="name(.)"/>
        </xsl:call-template>
      </xsl:message>
 </xsl:template>

 <xsl:template name="output_message3_fail">
    <xsl:param name="current_node"/>
    <xsl:text>
      Conversion to EML2.1 failed:
      The EML 2.0.1 document has empty string value in some elements which shouldn't be empty string in EML 2.1.0 sepcification - </xsl:text>
    <xsl:value-of select="$current_node"/>
</xsl:template>    

I use <xsl:message> to generate the error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM