java - 使用 Java 应用 XSL 样式表,输出缩进不正确

标签 java xslt format output indentation

请注意,我对 Java、XML 和 XSL 还很陌生,所以请耐心等待。 :)

我正在做一项大学作业,要求我使用 Java 合并两个 XML 文件。

虽然仅使用 XSL 会更直接,但我的任务是使用 Java 组合所述文件并按日期属性的顺序对 XML 文档的各个部分进行排序。

所以;我的组合工作正常,但我正在尝试格式化我的输出并对有问题的部分进行排序。

在我的 Java 代码中,我必须从最终(组合的)输出 XML 文件中删除某些元素;我一直在使用 x.getParentNode().removeChild(x) 来执行此操作。

这让我到处都是空白文本节点,因此我需要在最后阶段重新格式化以下 XSL:

<!-- Code removed due to possible plagiarism -->

现在这是奇怪的部分。如果我将此 XSL 应用于 Java 代码手动生成的输出文档,它会按预期工作并给出完美的结果。

但是,如果我尝试将 XSL Java 一起应用,它确实会删除空格,但不会为输出文档提供正确的缩进。下面的代码显示了我的意思(为了清楚起见,输出 XML 文件已被缩短):

手动应用时的正确输出:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE authors
  SYSTEM "output.dtd">
<authors>
   <author>
      <name>AMADEO, Giovanni Antonio</name>
      <born-died>b. ca. 1447, Pavia, d. 1522, Milano</born-died>
      <nationality>Italian</nationality>
      <biography>Giovanni Antonio was....</biography>
      <artworks form="architecture">
         <artwork date="1473">
            <title>Façade of the church</title>
            <technique>Marble</technique>
            <location>Certosa, Pavia</location>
         </artwork>
      </artworks>
   </author>
</authors>

与 Java 一起应用时的输出(使用 Transformer 类):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE authors SYSTEM "output.dtd">
<authors>
<author>
<name>AMADEO, Giovanni Antonio</name>
<born-died>b. ca. 1447, Pavia, d. 1522, Milano</born-died>
<nationality>Italian</nationality>
<biography>Giovanni Antonio Amadeo was an Italian early Renaissance sculptor, architect, and engineer. In 1466 he was engaged as a sculptor, with his brother Protasio, at the famous Certosa, near Pavia. He was a follower of the style of Bramantino of Milan, and he represents, like him, the Lombard direction of the Renaissance. He practised cutting deeply into marble, arranging draperies in cartaceous folds, and treating surfaces flatly even when he sculptured figures in high relief. Excepting in these technical points he differed from his associates completely, and so far surpassed them that he may be ranked with the great Tuscan artists of his time, which can be said of hardly any other North-Italian sculptor.</biography>
<artworks form="architecture">
<artwork date="1473">
<title>Façade of the church</title>
<technique>Marble</technique>
<location>Certosa, Pavia</location>
</artwork>
</artworks>
</author>

请注意,完全没有缩进?

这是我的 Java 代码,用于保存输出并应用 XSL:

// Code removed due to possible plagiarism.

注意:

  • srcDoc1 是我合并的 XML 文档(该程序基本上从 srcDoc2 中提取内容并将其放入 srcDoc1 中)。

过去几天我一直在敲键盘,真的需要一些建议来解释为什么会出现这样的情况。

提前致谢!

最佳答案

我们在评论线程中确定您在 Eclipse 项目中加载 Saxon,但在独立 Java 应用程序中加载 Xalan。

关于java - 使用 Java 应用 XSL 样式表,输出缩进不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42016645/

相关文章:

java - JDK 的 try-with-resources 示例中的错误做法?

xml - XSLT 是否支持 Clark 表示法?

xslt - XSL 中每组值的最大总和

java - 如何在 XSLT 2.0 中获取当前 xml 文件名?

python - 在python中将001格式的数字添加到文件名中

java - 将C代码改成JAVA代码【神经网络】

java - 无法构建 nosqlunit-mongodb 项目

java - 在 Windows swt 应用程序中创建安装程序时,框架图像不会更改

java - java中的txt文件格式验证

c++ - 如何计算格式化wchar数组所需的大小, `CRT_SECURE`方式?