KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Assigning DOCTYPE to XML documents
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: August 5, 2010

4D v12 offers a new command, DOM Append XML child node, which allows developers to do new and powerful things with 4D's DOM XML commands. One of the new features includes the ability to add a DOCTYPE to XML documents created with 4D's DOM XML commands.

The following sample code shows how to create a simple XML document with root, one element and a DOCTYPE:

C_TEXT(ref;temp;root)
root:=DOM Create XML Ref("MyRoot")
ref:=DOM Append XML child node(root;XML DOCTYPE;"MyRoot SYSTEM \"mydoctype.DTD\"")
ref:=DOM Create XML element(root;"myElement")
DOM SET XML ELEMENT VALUE(ref;"Hello")
temp:=DOM Create XML element(ref;"br")
temp:=DOM Append XML child node(ref;XML DATA;"New")
temp:=DOM Create XML element(ref;"br")
temp:=DOM Append XML child node(ref;XML DATA;"York")
DOM EXPORT TO FILE(root;"MyDoc.xml")
DOM CLOSE XML(root)


In 4D v12 this creates the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE MyRoot SYSTEM "mydoctype.DTD">
<MyRoot>
  <myElement>Hello
    <br/>New
    <br/>York
  </myElement>
</MyRoot>


To acheive the same XML document (with the DOCTYPE) in 4D v11 SQL you would need to use and XSL Transform. The following code is exactly the same as above, with the DOM Append XML child node command commented out. Then at the end of the code a call is made to XSLT APPLY TRANSFORMATION to include the DOCTYPE:

C_TEXT(ref;temp;root)
root:=DOM Create XML Ref("MyRoot")
 //ref:=DOM Append XML child node(root;XML DOCTYPE;"MyRoot SYSTEM \"mydoctype.DTD\"")
ref:=DOM Create XML element(root;"myElement")
DOM SET XML ELEMENT VALUE(ref;"Hello")
temp:=DOM Create XML element(ref;"br")
temp:=DOM Append XML child node(ref;XML DATA;"New")
temp:=DOM Create XML element(ref;"br")
temp:=DOM Append XML child node(ref;XML DATA;"York")
DOM EXPORT TO FILE(root;"MyDoc.xml")
DOM CLOSE XML(root)

C_TEXT($xml_doc;$xsl;$xml_result)
$xml_doc:="MyDoc.xml"
$xsl:="myXSL.xsl"
$xml_result:="post_transform.xml"
XSLT APPLY TRANSFORMATION($xml_doc;$xsl;$xml_result)


The myXSL.xsl document used here is:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" indent="no"
doctype-system="mydoctype.dtd"
/>
<xsl:template match="/">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>


Using this XSL transformation is the "v11" way to acheive the same thing you could do with one line of code in 4D v12.