[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Answer: XSLT Question: Inserting a DOCTYPE decl

  • From: "Roger L. Costello" <costello@m...>
  • To: xml-dev@i...
  • Date: Fri, 10 Dec 1999 07:37:25 -0500

doctype xsl
Hi Folks,

The solution to the problem that I posed of writing a stylesheet which
inserts a DOCTYPE declaration into the input XML document, where the DTD
file is found as the text value of an element in the input XML file, is
listed below.  Thanks to all those who made suggestions, particularly
Michael Kay who created the below solution.

                     DoctypeInserter.xsl
------------------------------------------------------------------
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
 
    <xsl:variable name="doctype">
        <xsl:value-of select="//DoctypeFile"/>
    </xsl:variable>

    <xsl:variable name="rootnode">
        <xsl:value-of select="name(*)"/>
    </xsl:variable>

    <xsl:template match="/">
        <xsl:variable name="quote">"</xsl:variable>  
        <xsl:value-of disable-output-escaping="yes" 
             select="concat('&lt;!DOCTYPE ', $rootnode, ' SYSTEM ', 
                            $quote, $doctype, $quote, '&gt;')"/>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="*|@*|comment()|
                         processing-instruction()|text()">
        <xsl:copy>
            <xsl:apply-templates select="*|@*|comment()| 
                                    processing-instruction()|text()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
------------------------------------------------------------------

As you can see, the trick was not to use xsl:output at all. Instead, the
DOCTYPE delcaration must be built "by hand".  After the DTD file is
found (and stored in doctype) the root node is found (and stored in
rootnode).  In the template rule for the document, before any of the XML
elements are copied, the DOCTYPE declaration is constructed by
concatenating together the various components.

With this XML document as input:

<?xml version="1.0"?>
<Numbers>
        <DoctypeFile>Number.dtd</DoctypeFile>
        <Number>27</Number>
        <Number>34</Number>
        <Number>18</Number>
        <Number>67</Number>
        <Number>99</Number>
        <Number>16</Number>
</Numbers>

The result of running it through the above stylesheet is:

<?xml version="1.0"?>
<!DOCTYPE Numbers SYSTEM "Number.dtd">
<Numbers>
        <DoctypeFile>Number.dtd</DoctypeFile>
        <Number>27</Number>
        <Number>34</Number>
        <Number>18</Number>
        <Number>67</Number>
        <Number>99</Number>
        <Number>16</Number>
</Numbers>

Which is, of course, exactly what I wanted!  

Thanks again.  /Roger


xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@i...
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To unsubscribe, mailto:majordomo@i... the following message;
unsubscribe xml-dev
To subscribe to the digests, mailto:majordomo@i... the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@i...)



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.