1
votes

I want to use Java to embed an XSL Stylesheet internally in an existing XML Document. I found this answer saying it is possible: Embed xsl into an XML file

But I'm using this format, since the example above wasn't working for me. It is pasted below as an example. http://www.calebmadrigal.com/embedding-xslt-xml-document/

    <?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xml" href="#stylesheet"?>
<!DOCTYPE catalog [
<!ATTLIST xsl:stylesheet
  id    ID  #REQUIRED>
]>
<catalog>
<xsl:stylesheet id="stylesheet" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog>

I understand you can link to an EXTERNAL stylesheet with a line such as href="MyStyle.xsl" in the xml, but this requires the user to have that file in the same folder as the output xml file.

I would like to embed this stylesheet INTERNALLY so it is viewable in Chrome as the 2nd link above indicates. I'm having problems doing this in java. I'm using the API Eclipse.

I have tried just writing the xsl to the xml document line by line (kind of cutting and pasting the xml document together), but this seems to disrupt with the reading of said document when using a DOM parser, and it's not very elegant. Is this possible?

1
Do you need help with Java code generating the XML with the embedded stylesheet? Which Java Api do you use?Martin Honnen
Thanks for the response. Yes, I could use some assistance with that. Right now I'm just trying to write out line by line to format it. I am in Eclipse. My program reads in an XML file, modifies some elements, and needs to output an XML file with the internally embedded stylesheet. I read about that XMLStreamWriter, but I am using a transformer factory to write the DOM object.Alex
Using XSLT to write a DOM tree to a file would not even output an internal DTD I think, even if you manage to get it into the DOM tree, which is also difficult as w3.org/TR/DOM-Level-3-Core/… is readonly, so unless you start by loading an XML document with the right DOCTYPE I don't see how DOM would allow you to insert it by code. You might want to look into other tree implementations like JDOM 2 which has jdom.org/docs/apidocs/org/jdom2/…. XOM also has a similar method.Martin Honnen
An error I'm having is when I close off the entire file with </catalog>, the line <!DOCTYPE catalog [ <!ATTLIST xsl:stylesheet id ID #REQUIRED> ]> no longer shows up. Can you tell me how to remedy this?Alex
I don't know of a way with a transformer respectively with XSLT to write out a DOCTYPE with an internal subset, other than using Saxon which has an extension to do that. That is why I suggested to use JDOM or XOM instead of DOM and a Transformer. And you will have to edit your question and show the relevant code if you want specific help.Martin Honnen

1 Answers

0
votes

If you use http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLStreamWriter.html#writeDTD(java.lang.String) you can write out the DOCTYPE node you need. I don't think the W3C DOM API allows you to create a DOCTYPE node with the embedded element and attribute declarations.