Skip to content

XML Prolog

Ozgur Ozcitak edited this page Mar 17, 2016 · 10 revisions

The XML prolog is usually created with the create function.

var root = require('xmlbuilder').create('xbel',
                     { version: '1.0', encoding: 'UTF-8'},
                     { pubID: '+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML', 
                       sysID: 'http://www.python.org/topics/xml/dtds/xbel-1.0.dtd'
                     }
                  );

The resulting XML will be:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML"
                      "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">
<xbel/>

XML Declaration

The XML declaration can be created at a later time by calling declaration from anywhere in the document (can also be abbreviated to dec).

builder.create('root')
  .dec('1.0', 'UTF-8', true)
  .ele('node');

The resulting XML will be:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
  <node/>
</root>

The dec function will return the root node.

Document Type Definition

The DTD can also be created at a later time by calling doctype from anywhere in the document (can also be abbreviated to dtd).

var dtd = root.dtd('pubID', 'sysID');

Calling dtd with both a public identifier and a system identifier will create a public DTD:

builder.create('HTML')
  .dtd('-//W3C//DTD HTML 4.01//EN', 
       'http://www.w3.org/TR/html4/strict.dtd');

The resulting XML will be:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">

Calling dtd with a single argument (system identifier) will create a private DTD:

builder.create('math')
  .dtd('http://www.w3.org/Math/DTD/mathml1/mathml.dtd')

The resulting XML will be:

<!DOCTYPE math SYSTEM "http://www.w3.org/Math/DTD/mathml1/mathml.dtd">

Calling dtd without arguments will create an empty DTD:

builder.create('html').dtd();
<!DOCTYPE html>

The dtd function will return the DTD object. To continue building the XML nodes, either up or root can be called to go back to the root node.

DTD Internal Subset

An internal subset can be created by calling declaration functions described below inside the dtd.

builder.create('html')
  .dtd()
    .com('internal subset')
    .ele('body', '(#PCDATA)')
  .up()
  .ele('body');

The resulting XML will be:

<?xml version="1.0"?>
<!DOCTYPE html [
  <!-- internal subset -->
  <!ELEMENT body (#PCDATA)>
]>
<html>
  <body/>
</html>
Element Type Declarations

Element type declarations can be created by calling element inside the DTD (can also be abbreviated to ele).

dtd.ele('title', '(#PCDATA)');

ele will return its parent DTD object.

Attribute-List Declarations

Attribute-list declarations can be created by calling attList inside the DTD (can also be abbreviated to att).

dtd.att('img', 'height', 'CDATA', '#REQUIRED');
dtd.att('img', 'visible', '(yes|no)', '#DEFAULT', 'yes');

att will return its parent DTD object.

Entity Declarations

General entity declarations can be created by calling entity inside the DTD (can also be abbreviated to ent).

dtd.ent('entityname', 'value');
dtd.ent('entityname', { sysID: 'http://www.myspec.com/ent' });
dtd.ent('entityname', { pubID: '-//MY//SPEC ENT//EN', sysID: 'http://www.myspec.com/ent', nData: 'entprg' });

Paremeter entity declarations can be created by calling pEntity inside the DTD (can also be abbreviated to pent).

dtd.pent('entityname', 'value');
dtd.pent('entityname', { sysID: 'http://www.myspec.com/ent' });
dtd.pent('entityname', { pubID: '-//MY//SPEC ENT//EN', sysID: 'http://www.myspec.com/ent' });

ent and pent will return the parent DTD object.

Notation Declarations

Notation declarations can be created by calling notation inside the DTD (can also be abbreviated to not).

dtd.not('fs', { sysID: 'http://my.fs.com/reader' });
dtd.not('fs-nt', { pubID: 'FS Network Reader 1.0', sysID: 'http://my.fs.com/reader' });

not will return its parent DTD object.

CDATA, Processing Instructions and Comments

CDATA, processing instruction and comment nodes can be created inside the DTD by calling dat, ins and com respectively. Their usage is described in the wiki.

Ending the DTD

Once the DTD is built, root or up is called to return to the XML root node and continue building the XML document.

dtd.root();