2017-01-27 77 views
0

我使用Altova StyleVision生成和XSLT-FO模板,當我生成XSLT 1.0 FO文件時,我成功地使用Apache FOP轉換爲PDF代碼如下。使用Apache FOP和風格視覺的Saxon Java中的XSLT 2.0轉換

 DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); 
     Configuration cfg = cfgBuilder 
       .buildFromFile(new File("fop.xconf")); 
     FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(
       new File("fopbase").toURI()).setConfiguration(cfg); 
     FopFactory fopFactory = fopFactoryBuilder.build(); 

     File xsltFile = new File(
       "xslt1File.xslt"); 
     StreamSource xmlSource = new StreamSource(
       new File("xmlData.xml")); 

     FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 
     OutputStream out; 
     out = new java.io.FileOutputStream("GeneratedPDF.pdf"); 
     try { 
      Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 
      TransformerFactory factory = TransformerFactory.newInstance(); 
      Transformer transformer = factory.newTransformer(new StreamSource(xsltFile)); 
      Result res = new SAXResult(fop.getDefaultHandler()); 
      transformer.transform(xmlSource, res); 

     } finally { 
      out.close(); 
     } 

此代碼很好用。

XSLT-FO 2.0

問題一直試圖使用XSLT 2.0的功能,當我生成StyleVision XSLT-FO 2.0模板。

我看過一篇文章HereHere所以我下載了saxon9.jar [並將其添加到構建路徑],我改變了變壓器廠就像下面

 DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); 
    Configuration cfg = cfgBuilder 
      .buildFromFile(new File("fop.xconf")); 
    FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(
      new File("fopbase").toURI()).setConfiguration(cfg); 
    FopFactory fopFactory = fopFactoryBuilder.build(); 

    File xsltFile = new File(
      "xslt1File.xslt"); 
    StreamSource xmlSource = new StreamSource(
      new File("xmlData.xml")); 

    FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 
    OutputStream out; 
    out = new java.io.FileOutputStream("GeneratedPDF.pdf"); 
    try { 
     Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 
     TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl(); 
     Transformer transformer = factory.newTransformer(new StreamSource(xsltFile)); 
     Result res = new SAXResult(fop.getDefaultHandler()); 
     transformer.transform(xmlSource, res); 

    } finally { 
     out.close(); 
    } 

的TransformerFactory廠=新net.sf .saxon.TransformerFactoryImpl();

當我運行代碼,我得到一個錯誤

Error at xsl:import-schema on line 12 column 67 of xsltfile.xslt: 
    XTSE1650: xsl:import-schema requires Saxon-EE 
javax.xml.transform.TransformerConfigurationException: net.sf.saxon.s9api.SaxonApiException: xsl:import-schema requires Saxon-EE 
    at net.sf.saxon.jaxp.SaxonTransformerFactory.newTemplates(SaxonTransformerFactory.java:157) 
    at net.sf.saxon.jaxp.SaxonTransformerFactory.newTransformer(SaxonTransformerFactory.java:110) 
    at main.FopTransformer.convertToPDF(FopTransformer.java:60) 
    at main.FopTransformer.main(FopTransformer.java:29) 

是否有任何你們知道一個完整的解決方案,使用XML轉換XSLT + 2.0 PDF在Java撒克遜HE。

任何幫助將不勝感激。

謝謝。

回答

2

有沒有人知道一個完整的解決方案,它將xml + xslt 2.0轉換爲使用saxon HE的PDF格式的PDF。

錯誤消息非常明確:如果您要使用架構感知型XSLT 2.0,則需要Saxon-EE。你的問題的答案是:

如果你想用Saxon-HE來做,那麼你的樣式表不能使用xsl:import-schema

+0

嗨,對於後期的relpy抱歉,我添加了saxon9ee.jar,但仍然有同樣的錯誤。 – IsaacK

+0

我的天啊。我不相信我有XSLT 2.0和XPath 2.0第4版的作者的迴應。感謝您幫助我解決問題,儘管saxon9ee.jar仍然失敗。 – IsaacK

+0

除了將Saxon-EE jar放在類路徑中,您需要使用'TransformerFactory factory = new com.saxonica.config.EnterpriseTransformerFactory();' –