XSLT Question: Inserting a DOCTYPE decl

Roger L. Costello costello at mitre.org
Thu Dec 9 20:59:29 GMT 1999


Hi Folks,

I have a situation where I have many XML documents that do not contain a
DOCTYPE declaration, and would like to write a stylesheet that inserts a
declaration within the documents.  The interesting aspect of this
problem is that each XML document contains within it an element which
gives the name of the DTD file.  So, the declaration should use the
value of that element as the name for the DTD file.

For example, here's a sample XML document into which I need to insert a
DOCTYPE declaration:

<?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>

Note the DoctypeFile element, which indicates the name of the DTD file.

The stylesheet should insert the declaration, thus resulting in an XML
document as such:

<?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>

Here's the stylesheet that I wrote to do this task:

<?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:output method="xml" doctype-system="string($doctype)"/>

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

</xsl:stylesheet> 

A pretty simple stylesheet - create a variable which gets the value of
the DoctyleFile element, and instruct the xsl:output element to output a
DOCTYPE declaration, using the value of the variable as the name of the
DTD file, and then do a copy operation on the input XML document.

Here is the XML file that I get when this example is run through XT
(Lotus XSL gives the same results):

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

Note that the XSL processor did not evaluate the expression that I used
in the xsl:output's doctype-system attribute.  Instead, it used the
expression literally.

Thus, here are my questions:

(1)  Is this a bug in XT and Lotus XSL?
(2)  I suspect it isn't a bug, in which case can someone think of
another way to solve this problem?

/Roger


xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at 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 at ic.ac.uk the following message;
unsubscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list