使用 Java 应用 XSL 样式表,输出缩进不正确
Applying XSL Stylesheet with Java, output is not indented correctly
请注意,我是 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时的输出(使用转换器class):
<?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、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时的输出(使用转换器class):
<?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。