Solved

Using XSLT (xalan) to add <!DOCTYPE ...> headers - probably obvious

Posted on 2004-04-06
8
946 Views
Last Modified: 2013-11-18
I am sure that I am missing something in xslt syntax, or a well known trick of the environment.

I want to add a DOCTYPE header to my document during its XSLT transform.  However, all of the approaches I have tried have either given me parsing errors on the XSLT script or have not produced output.

Currently, I have a comment at the right place that says (details have been edited):

<!--  Figure out how to attach the DTD descriptor with XSLT
    <!DOCTYPE MyMessageBatch PUBLIC "-//xxx//yyy 3.1//EN" "http://xxx.xxx.com/DTD/yyy_3.1.dtd">
-->

Thanks in advance.
0
Comment
Question by:swift99
  • 5
  • 2
8 Comments
 
LVL 26

Accepted Solution

by:
rdcpro earned 125 total points
ID: 10768938
Use the <xsl:output> tag to specify the PUBLIC and SYSTEM identifiers:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output
            method="xml" version="1.0" encoding="UTF-8" indent="yes"
            doctype-public="-//xxx//yyy 3.1//EN"
            doctype-system="http://xxx.xxx.com/DTD/yyy_3.1.dtd"
            omit-xml-declaration="yes" />

The processor should use your root element as the DOCTYPE name, and enter the PUBLIC and system identifiers for you.

Regards,
Mike Sharp
0
 
LVL 6

Author Comment

by:swift99
ID: 10769020
Thanks, but I still get no DOCTYPE header.

The output document is still (approximately)

<?xml version="1.0" encoding="UTF-8"?>
<MyMessageBatch>... stuff ...</MyMessageBatch>


Any other suggestions?  This is being called from within a SAX based framework.  Might this be a problem with our framework's initialization or usage of the xalan transformer?
0
 
LVL 15

Assisted Solution

by:dualsoul
dualsoul earned 125 total points
ID: 10769396
hm...strange this works fine with Xalan.

i've tested Mikes suggestion this way:

Java code: (using Xalan 2.5.1)
............................................................................
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class SimpleTransform {
      public static void main(String[] args) throws Exception  {  
            TransformerFactory tFactory = TransformerFactory.newInstance();
            //Transformer with identity transformation
            Transformer transformer = tFactory.newTransformer(new StreamSource("indent.xsl"));
            
            String xml="<root><a><b><c>aaa</c></b></a></root>";
            StringWriter result=new StringWriter();
            //Transfrom xml (in String) to formatted xml (in String)                        
            transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(result));
            //Output result            
            System.out.println(result.toString());
        }
}
................................................................................

XSLT:
..............................................................
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output
            method="xml" version="1.0" encoding="UTF-8" indent="yes"
            doctype-public="-//xxx//yyy 3.1//EN"
            doctype-system="http://xxx.xxx.com/DTD/yyy_3.1.dtd"
            omit-xml-declaration="yes" />
           
            <xsl:template match="/">
                  <xsl:copy-of select="."/>
            </xsl:template>
</xsl:stylesheet>
...............................................................

and the output is:
...........................................................
<!DOCTYPE root PUBLIC "-//xxx//yyy 3.1//EN" "http://xxx.xxx.com/DTD/yyy_3.1.dtd">
<root>
      <a>
            <b>
                  <c>aaa</c>
            </b>
      </a>
</root>
...........................................................

can you tell us more about your envioronment? Xalan parameters you are using?
0
 
LVL 6

Author Comment

by:swift99
ID: 10769472
I think that narrows it down.

I'll have to go digging into our internal framework classes and see what we're doing to xalan to get this behavior.

If I find the problem in our code I'll increase and split the points (50% for the solution, and 50% for isolating the problem).  Otherwise, I'll be back in a day or so with more information.

Thanks!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Author Comment

by:swift99
ID: 10775635
I hunted around, with the information you gents gave me, and I think I found the problem.

Once again I was broad-sided by a complex framework that mixes technologies, and the problem was in a place other than where I thought it was.

The xalan piece is embedded in a SAX filter.  If I am reading my books correctly, SAX version 1 does not understand anything with "<!" tags (DOCTYPE, CDATA, etc), so the xalan piece is generating correct XML but one of the SAX pieces is not understanding the tag and is skipping it.

Do you concur that my permanent solution would most likely be to upgrade the SAX portion of the process to SAX 2, which allegedly supports these constructs?
0
 
LVL 6

Author Comment

by:swift99
ID: 10775902
I've also confirmed now that if I send the output to a stream the DOCTYPE header is correct.  

If I send the output to the SAX piece the result has no DOCTYPE header.
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 10776205
That sounds like a reasonable approach to me, though I don't know much about your architecture, unless upgrading to SAX 2 would cause problems with your app.  Hard to imagine it would, though.   I assume the SAX piece is necessary?  That is, you can't simply use the stream?

Regards,
Mike Sharp
0
 
LVL 6

Author Comment

by:swift99
ID: 10776761
There's a long chain of transformations involved, and this piece is right in the middle of it all.  I will be discussing my options with the original designer/builder of the framework and process as soon as he is available.

SAX 2 should not cause problems - it deprecates some SAX1 interfaces but does not remove them.

Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now