我正在使用Oracle数据库内的xslt和java扩展名转换xml
当我用xsl解析xml时出现错误:
errorjavax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:858)
我没有扩展xslt进行了测试,它可以正常工作。
另外,我已经在其他环境中测试了转换,并且效果很好。
Java版本是1.6.0_43。
xslt是:
<xsl:stylesheet version="1.0"
xmlns:java="http://xml.apache.org/xalan/java/XsltTransformer"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="APSDocument">
<atag value='{java:GetDateTimeNow()}' ></atag>
</xsl:template>
</xsl:stylesheet>
XML:
<APSDocument Tag="APP002">
<Section Tag="APPLICATION_FINISHED">
<SectionBody>
<Field Tag="APP_FINISHED">1</Field>
</SectionBody>
</Section>
</APSDocument>
java类:
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "XsltTransformer" AS
import java.io.*;
import java.text.*;
import java.util.Date;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.sql.*;
public class XsltTransformer
{
public static String TransformToSvTLV(String xmlDoc, String xsltDoc) throws Exception
{
try
{
XsltTransformer xsltTransformer = new XsltTransformer();
Document svmlDoc = xsltTransformer.TransformToSvML(xmlDoc, xsltDoc);
return xsltTransformer.TransformSvMLToStTlv(svmlDoc);
} catch (Exception e)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
}
private Document TransformToSvML(String xmlDoc, String xsltDoc) throws Exception
{
TransformerFactory tFactory=TransformerFactory.newInstance();
Source xslSourceDoc=new StreamSource(new StringReader(xsltDoc));
Source xmlSourceDoc=new StreamSource(new StringReader(xmlDoc));
StringWriter writer = new StringWriter();
Transformer trasform=tFactory.newTransformer(xslSourceDoc);
trasform.transform(xmlSourceDoc, new StreamResult(writer));
//System.out.println(writer.toString());
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(writer.toString())));
doc.getDocumentElement().normalize();
return doc;
}
public static String GetDateTimeNow()
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
return simpleDateFormat.format((new Date())).replace('/', '.');
}
private String TransformXmlNodeToSvTlv(Node svmlNode, String endChar)throws Exception
{
return "do nothing";
}
}
调用TransformToSvTLV方法:
FUNCTION EtlTransformToSvXML(xmlDoc LONG,xsltDoc LONG) RETURN VARCHAR2
IS LANGUAGE JAVA NAME
'XsltTransformer.TransformToSvTLV(java.lang.String, java.lang.String)
return java.lang.String';
提前致谢!
最佳答案
在这种情况下,最可能的问题是程序无法读取xslt文件。
我建议的可能解决方案是使用
new StreamSource(new File("path/to/your/xslt"))