Solved

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

Posted on 2004-04-06
8
969 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

738 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