0

当我尝试在我的 Web 应用程序中使用 xslt 转换 xml 时,Weblogic 10.3 服务器中会引发 TransformerConfigurtionException 异常。相同的 Web 应用程序代码在 Tomcat 7.0 中运行良好。我不知道是什么原因导致此异常。

Exception:
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
ERROR: 'Syntax error in 'format-date($date,'[MNn][D],[Y]','en',(),())'.'
FATAL ERROR: 'Could not compile stylesheet'

在 xslt 中删除 format-date 函数后,我得到了另一个异常(javax.xml.tranform.TransformerException:java.lang.ArrayIndexOutOfBoundsException

代码:

TransformerFactory factory = TransformerFactory.newInstance();

  try
  {
   factory.newTransformer().transform( new StreamSource( new StringReader( xml ) ), new StreamResult( transformResult ) );

   Source documentInfoSource = new StringSource( new String( transformResult.toByteArray() ) );
   transformResult.reset();

   factory.setURIResolver( new URIResolver()
   {
    @Override
    public Source resolve(String href, String base) throws TransformerException
    {
     try
     {
      return new StreamSource( EcrionDocumentRenderServiceImpl.class.getClassLoader().getResourceAsStream( href ) );
     }

     catch( Exception e )
     {
      throw new TransformerException( e );
     }
    }

   } );
   factory.newTransformer( new StreamSource( Thread.currentThread().getContextClassLoader().getResourceAsStream( "template.xsl" ) ) ).transform( documentInfoSource, new StreamResult( transformResult ) );TransformerFactory factory = TransformerFactory.newInstance();

  try
  {
   factory.newTransformer().transform( new StreamSource( new StringReader( xml ) ), new StreamResult( transformResult ) );

   Source documentInfoSource = new StringSource( new String( transformResult.toByteArray() ) );
   transformResult.reset();

   factory.setURIResolver( new URIResolver()
   {
    @Override
    public Source resolve(String href, String base) throws TransformerException
    {
     try
     {
      return new StreamSource( EcrionDocumentRenderServiceImpl.class.getClassLoader().getResourceAsStream( href ) );
     }

     catch( Exception e )
     {
      throw new TransformerException( e );
     }
    }

   } );
   factory.newTransformer( new StreamSource( Thread.currentThread().getContextClassLoader().getResourceAsStream( "template.xsl" ) ) ).transform( documentInfoSource, new StreamResult( transformResult ) );
4

1 回答 1

1

format-date()是一个 XSLT 2.0 函数。当您使用 JAXP TransformerConfigurationFactory 加载 XSLT 转换器时,您无法控制返回的处理器是 XSLT 1.0 还是 2.0 处理器 - 这取决于在类路径中找到的内容。我的猜测是类路径上没有 XSLT 2.0 处理器,因此已加载默认的内置 Xalan 处理器,这不支持 XSLT 2.0(因此不支持format-date())。

如果您的代码依赖于 XSLT 2.0,那么

a) 确保 Saxon 在类路径中

b)通过替换显式加载它

TransformerFactory factory = TransformerFactory.newInstance();

TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
于 2014-01-10T08:32:29.077 回答