Solved

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

Posted on 2004-04-06
8
954 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path of Workbook 3 79
How to read XML file attributes... 17 63
AL3 Files 4 29
Powershell XML in variable 4 21
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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:

832 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