Skip to content

Value Conversion

Ozgur Ozcitak edited this page Feb 18, 2019 · 2 revisions

This page documents string conversion functions used while converting user supplied values to XML nodes. Those functions are passed to the builder while calling the create and begin functions:

var xmlbuilder = require('xmlbuilder');

var root = xmlbuilder.create('root', {
  stringify: {
    name: function(val) {
      return 'myns:' + val; // prepend myns: to all names
    }
  }
});

name: function(val)

converts val to a node name or an attribute name string

text: function(val)

converts val to text node content

cdata: function(val)

converts val to CDATA node content

comment: function(val)

converts val to comment node content

raw: function(val)

converts val to raw node content

attValue: function(val)

converts val to attribute content

insTarget: function(val)

converts val to processing instruction node target

insValue: function(val)

converts val to processing instruction node value

xmlVersion: function(val)

converts val to XML version string

xmlEncoding: function(val)

converts val to XML encoding string

xmlStandalone: function(val)

converts val to XML standalone string

dtdPubID: function(val)

converts val to DocType public identifier

dtdSysID: function(val)

converts val to DocType system identifier

dtdElementValue: function(val)

converts val to element (!ELEMENT) node content inside the DTD

dtdAttType: function(val)

converts val to attribute (!ATTLIST) node type inside the DTD

dtdAttDefault: function(val)

converts val to attribute (!ATTLIST) node default value inside the DTD

dtdEntityValue: function(val)

converts val to entity (!ENTITY) node value inside the DTD

dtdNData: function(val)

converts val to attribute (!NOTATION) node data inside the DTD

textEscape: function(str)

escapes str to be used in text node value

attEscape: function(str)

escapes str to be used in attribute value

Decorators

While converting from JS objects, XML attributes and special nodes are recognized with decorator strings. For example, if an object key starts with @, it will be converted to an XML attribute. Decorators can also be customized by setting stringify properties below.

convertAttKey: '@'

When prepended to a JS object key, converts the key-value pair to an attribute.

convertPIKey: '?'

When prepended to a JS object key, converts the key-value pair to a processing instruction node.

convertTextKey: '#text'

When prepended to a JS object key, converts its value to a text node. Since JS objects cannot contain duplicate keys, multiple text nodes can be created by adding some unique text after each object key. For example:

obj = {
  node: {
    '#text_1': 'some text',
    '#text_2': 'additional text',
    '#text_3': 'some more text',
  }
}

This also applies to CDATA, comment and raw keys below.

convertCDataKey: '#cdata'

When prepended to a JS object key, converts its value to a CDATA node.

convertCommentKey: '#comment'

When prepended to a JS object key, converts its value to a comment node.

convertRawKey: '#raw'

When prepended to a JS object key, converts its value to a raw text node.