有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Saxon和XSLT转换JDOM XML文档

我正在尝试转换一些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文件,其中使用连字符entitiy字符串代替连字符。但是,我需要自动化这个过程,所以我使用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使用它有关。有人能帮忙吗

非常感谢

吉姆


共 (1) 个答案

  1. # 1 楼答案

    问题的确在于没有使用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);