1

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!

Daniel
  • 13
  • 4
  • Would you please state the used XSLT Processor? Xalan, Saxon, some other?! – uL1 Feb 03 '17 at 05:20
  • Does this help: http://stackoverflow.com/questions/2402212/how-do-i-make-xsl-transformation-indent-the-output ? – Michael Kay Feb 03 '17 at 09:21
  • @uL1, apologies, forgot to mention that. I'm using Saxon. – Daniel Feb 04 '17 at 11:51
  • @Michael Kay, thanks for the link, I already found that and got the same output. I'll try again in case I messed something up but I'm pretty sure I didn't. – Daniel Feb 04 '17 at 11:53
  • @MichaelKay, just tried it then, but no luck. The problem is that the XSL works fine if I apply it manually using eclipse; it's when I try to apply it through Java code using the transformer as shown in the code in my OP that it chucks a hissy fit. – Daniel Feb 04 '17 at 12:02
  • Check whether you really are using Saxon. See what class TransformerFactory.newInstance() has given you: I suspect it is Xalan. The most reliable (and fastest) way to be sure of loading Saxon is to ignore the JAXP search mechanism, replace `TransformerFactory.newInstance()` with `new net.sf.saxon.TransformerFactoryImpl()`. – Michael Kay Feb 04 '17 at 13:15
  • @MichaelKay, thanks very much, that solved it. It was a silly newbie mistake; I have set up _Eclipse_ to use Saxon to process XSL, but I hadn't set my actual Java project up to do so. Adding the Saxon jar file to the project classpath and using what you said there fixed it. How do I +1 or choose as correct answer on StackOverflow? – Daniel Feb 04 '17 at 14:06
  • I've summarised the conclusion as an answer which you can now accept by ticking the tick-mark. – Michael Kay Feb 04 '17 at 16:12
  • Sweet, done. Thanks again! – Daniel Feb 05 '17 at 16:40

1 Answers1

0

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

Michael Kay
  • 156,231
  • 11
  • 92
  • 164