0

I can't figure out how to add simple XML and Doctype declarations to an XML file I am generating. I'm creating and adding nodes fine but that code doesn't seem to work for doing this. My code is as follows:

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

Set objDoctype = xmlDoc.createElement("DOCTYPE")
xmlDoc.appendChild objTop

I found this link: http://www.example-code.com/vbscript/xml_doctype.asp which doesn't seem to work for me because it looks like they are editing the file as a TXT file.

Also, I need to add this declaration to the top of my XML file:

<?xml version="1.0" encoding="UTF-8"?>

Thanks for your help!

EDIT

What I would like to see on the top of the XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "shopstyle-catalog.dtd">

Thanks!

11
  • Please post a complete, but short example of the desired output (to clarify moot points: DOCTYPE? HTML? HTML5? XHTML? XML?) Commented Jul 16, 2014 at 16:42
  • @Ekkehard.Horner, I just updated that. Let me know if that helps clarify it. Thanks! Commented Jul 16, 2014 at 16:55
  • <?xml version="1.0" encoding="UTF-8"?> is, in fact, the default. It's not required unless your XML document has a different version or encoding. Commented Jul 16, 2014 at 16:55
  • @Tomalak, when I open the file after creating it and saving it to the server, it doesn't have the XML declaration on top. Commented Jul 16, 2014 at 17:01
  • So? The XML declaration is optional (unless the document does not conform to the default). Commented Jul 16, 2014 at 17:04

1 Answer 1

1

I found no easy way to add a doctype declaration to an existing document(*) via the XMLDOM/MSXML2 API. It appears to be impossible (see the "Remarks" section). Quote, emphasis mine:

You cannot create a node of type NODE_DOCUMENT, NODE_DOCUMENT_TYPE, NODE_ENTITY, or NODE_NOTATION.

However, this is a trivial task with XSLT. This tiny transformation adds an XML declaration and a doctype to your input document - plus it transcodes the input to any output encoding you specify (I strongly recommend leaving it at UTF-8, though).

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="no" encoding="UTF-8" method="xml" />
  <xsl:output doctype-system="shopstyle-catalog.dtd" />

  <xsl:template match="node() | @*">
    <xsl:copy><xsl:apply-templates select="node() | @*" /></xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The rest of the input document is copied verbatim.

There are countless examples of how to apply XSL stylesheets to XML documents in various languages and environments, so I'm not going to go into this here.


(*) If you create a document from scratch, there is a way: Instead of trying to create the doctype declaration programmatically, you can use the loadXML() method to load a skeleton XML string that already contains the wanted doctype declaration.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.