Daniel Daniel - 4 years ago 96
Java Question

Applying XSL Stylesheet with Java, output is not indented correctly

Just a heads up, I'm fairly new to Java, XML, and XSL, so bear with me. :)

I'm working on a uni assignment which has asked me to combine two XML files using Java.

While something like this would be much more straight-forward using XSL exclusively, my task was to use Java to combine said files and sort parts of the XML document in order of date attributes.

So; I have the combining working correctly but I'm trying to format my output and sort the parts in question.

In my Java code I have had to remove certain elements from the final (combined) output XML file; and I have been using x.getParentNode().removeChild(x) to do so.

This is leaving me with blank text nodes everywhere, so I have the following XSL to re-format at the final stage:

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


Now this is the weird part. If I apply this XSL to the output document that my Java code produces manually, it works as expected and gives me the perfect result.

However, if I try to apply the XSL with Java, it does strip the whitespace out, but it doesn't give the output document the correct indentation. The code below show what I mean (output XML file has been shortened for clarity):

Correct output when applied manually:

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


Output when applied with Java (using the Transformer 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>


Notice how there is absolutely no indentation?

Here is my Java code that is saving the output & applying the XSL:

// Code removed due to possible plagiarism.


Note:


  • srcDoc1 is the XML document that I have combined (the program basically pulled stuff out of srcDoc2 and placed it in srcDoc1).



I've been bashing my head on my keyboard for the last couple days and really need some advice as to why this is behaving like this.

Thanks in advance!

Answer Source

We determined in the comment thread that you were loading Saxon in your Eclipse project, but Xalan in your standalone Java application.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download