我正在尝试转换某些XML,以便iso8879实体字符串代替字符。例如,字符串1234-5678
将变为1234‐5678
。我使用字符映射和http://www.w3.org/2003/entities/iso8879doc/overview.html处的样式表来完成此操作。
我的xslt的第一部分看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="iso8879map.xsl"/>
<xsl:output omit-xml-declaration = "yes" use-character-maps="iso8879"/>
当我使用Saxon XSLT引擎在Eclipse中运行此样式表时,它可以正常工作并输出一个带有连字符权限字符串的XML文件来代替连字符。但是,我需要自动执行此过程,因此使用JDOM包。不幸的是,在转换过程中字符没有被替换。执行转换的代码看起来有点像这样:
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl"); // use saxon for xslt 2.0 support
SAXBuilder builder = new SAXBuilder();
builder.setExpandEntities(false);
XSLTransformer transformer = new XSLTransformer(styleSheet);
Document toTransform = builder.build(Fileref); // transform
Document transformed = transformer.transform(toTransform);
然后我使用以下方法将文档写入文件:
public static void writeXMLDoc(File xmlDoc, Document jdomDoc){
try {
Format format = Format.getPrettyFormat();
format.setOmitDeclaration(true);
format.setEncoding("ISO-8859-1");
XMLOutputter outputter = new XMLOutputter(format);
//outputter.output((org.jdom.Document) allChapters, System.out);
FileWriter writer = new FileWriter(xmlDoc.getAbsolutePath());
outputter.output((org.jdom.Document) jdomDoc, writer);
writer.close();
}
catch (java.io.IOException exp) {
exp.printStackTrace();
}
}
我已经开始在Eclipse中进行调试,看起来在xslt转换期间没有替换连字符。我已经使用它自己的Saxon xslt引擎对它进行了测试,它确实有效,所以它可能与使用Java和Jdom时有关。有人可以帮忙吗?
非常感谢。
吉姆
答案 0 :(得分:2)
问题确实是因为没有使用Saxon提供的JDOM包装器类。这是用于引用的工作代码,它显示了正在转换并作为新JDOM文档返回的JDOM文档:
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); // use saxon for xslt 2.0 support
File styleSheet = new File("filePath");
// Get a TransformerFactory
System.setProperty("javax.xml.transform.TransformerFactory",
"com.saxonica.config.ProfessionalTransformerFactory");
TransformerFactory tfactory = TransformerFactory.newInstance();
ProfessionalConfiguration config = (ProfessionalConfiguration)((TransformerFactoryImpl)tfactory).getConfiguration();
// Get a SAXBuilder
SAXBuilder builder = new SAXBuilder();
//Build JDOM Document
Document toTransform = builder.build(inputFileHandle);
//Give it a Saxon wrapper
DocumentWrapper docw = new DocumentWrapper(toTransform, inputHandle.getAbsolutePath(), config);
// Compile the stylesheet
Templates templates = tfactory.newTemplates(new StreamSource(styleSheet));
Transformer transformer = templates.newTransformer();
// Now do a transformation
ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);
transformer.transform(docw, new StreamResult(outStream));
ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
Document transformed = builder.build(inStream);