使用 Saxon 和 XSLT 转换 JDOM XML 文档

发布于 2024-10-29 17:45:55 字数 1935 浏览 0 评论 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"/>

当我使用 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 使用它有关。有人可以帮忙吗?

非常感谢。

吉姆

I'm trying to convert some XML so that iso8879 entity strings will appear in place of characters. For example the string 1234-5678 would become 1234‐5678. I've done this using character maps and the stylesheets found at http://www.w3.org/2003/entities/iso8879doc/overview.html.

The first part of my xslt looks like this:

<?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"/>

When I run this stylesheet in Eclipse with the Saxon XSLT engine it works fine and outputs an XML file with the hyphen entitiy string in place of the hyphen character. However, I need to automate this process so am using the JDOM package. Unfortunately, the characters are not being replaced during the transformation. The code that does the conversion looks a little like this:

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);

I then write the document out to a file using the following method:

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();
    }
}

I've started debugging in Eclipse and it looks like the hyphen character isn't being replaced during the xslt transformation. I've tested this using the Saxon xslt engine on it's own and it does work, so it's likely something to do with using it from Java and Jdom. Can anybody help?

Many thanks.

Jim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-11-05 17:45:56

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

The problem did turn out to be with not using the JDOM wrapper class provided by Saxon. Here's the working code for reference that shows a JDOM document being transformed and being returned as a new JDOM document:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文