OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Answer: XSLT Question: Inserting a DOCTYPE decl

[ Lists Home | Date Index | Thread Index ]
  • From: "Roger L. Costello" <costello@mitre.org>
  • To: xml-dev@ic.ac.uk
  • Date: Fri, 10 Dec 1999 07:37:25 -0500

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@ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To unsubscribe, mailto:majordomo@ic.ac.uk the following message;
unsubscribe xml-dev
To subscribe to the digests, mailto:majordomo@ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@ic.ac.uk)






 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS