0

我正在尝试转换一些 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"/>

当我在 Eclipse 中使用 Saxon XSLT 引擎运行此样式表时,它可以正常工作并输出一个带有连字符实体字符串代替连字符的 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 中使用它有关。有人可以帮忙吗?

非常感谢。

吉姆

4

1 回答 1

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);
于 2011-05-03T10:36:03.233 回答