user2383349
user2383349

Reputation:

Transforming XML with Java

I was learning how to convert an XML file into a HTML using just Java, then later I decided to learn how to use the XSLT language to do the same.


By saying just java, I mean, using just the syntax of the Java language, that is, not XSLT language.


To clarify:

  1. Loading XML into a DOM (using a DocumentBuilder).
  2. Parsing it (just doing things like doc.getFirstChild()).
  3. Writing it to a HTML file (just using a character stream, not a XML serialization).

What happened?

After I include the following line in my XML:

<?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?>

My Java application couldn't write the HTML right...

If I remove that, everything is right, but I want to keep it.

Any ideas how to ignore this "instruction"?

Upvotes: 0

Views: 126

Answers (3)

user2383349
user2383349

Reputation:

Sorry guys, I thought that the XML with the stylesheet.xsl was being "transformed" in the DOM object that I was using to parse the XML.

I made assumptions that:

  • The XML was being transformed before being put in the DOM.
  • The <?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?> was invisble in the DOM.

Basically I had a simple XML to start learning how to do the transformation. Something like the following:

<?xml version="1.0" encoding="UTF-8"?>
<items><item>...</item></items>

For simplicity (I was learning...) I decided to start my parsing with:

parse(doc.getFirstChild().getFirstChild()); //Expecting the first "item".

But after introducing the stylesheet to the XML the document became:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?>
<items><item>...</item></items>

Because of this introduction the doc.getFirstChild().getFirstChild() was not being a "item" anymore.

Then I just realize that I forgot to skip the node with this instruction (I really thought that it was "invisible" in the DOM tree).

Learning guys, learning...

P.S. That was my first attempt to transform a XML with XSLT!

Thank you for your help.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163675

XSLT will ignore processing instructions (that is, remove them) by default. If you want to retain this one, just add a template rule to do so:

<xsl:template match="processing-instruction('xml-stylesheet')">
  <xsl:copy/>
</xsl:template>

This assumes that your stylesheet is written in the classic recursive-descent style using apply-templates; if you're self-taught in XSLT then you might not have yet learnt this style. As always, it's much easier to help people when they show us the code.

Upvotes: 1

Borodin
Borodin

Reputation: 126772

It depends on how you are reading the XML from your Java application. But if your XML has an embedded Processing Instruction like

<?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?>

then it means that the stylesheet is an integral part of the data, and must be applied to the XML for it to be of any use. This is very similar to a CSS stylesheet processing instruction like, for example

<?xml-stylesheet type="text/css" href="standard.css"?>

which, in the same way, is an integral part of the XHTML, just as if it was an internal style within <style> tags.

It is clearly possible to read and use the XML without applying the stylesheet, but that is to ignore the directive of the data itself.

If you want to treat the XML as raw data and apply an optional transform to it in different ways then you must omit the processing instruction from the XML.

Upvotes: 0

Related Questions