[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Answer: XSLT Question: Inserting a DOCTYPE decl
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('<!DOCTYPE ', $rootnode, ' SYSTEM ', $quote, $doctype, $quote, '>')"/> <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! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|