Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-06
8
Medium Priority
?
1,010 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 500 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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…
Suggested Courses

927 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