Solved

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

Posted on 2004-04-06
8
981 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

635 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