使用 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"/>
当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题确实出在没有使用 Saxon 提供的 JDOM 包装类上。以下是供参考的工作代码,显示了正在转换的 JDOM 文档并作为新的 JDOM 文档返回:
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: