To avoid repeating long namespace URIs, you can use aliases.
Built-in Aliases
The following namespaces are built-in and can be used by prepending them with '@':
html: http://www.w3.org/1999/xhtmlxml: http://www.w3.org/XML/1998/namespacexmlns: http://www.w3.org/2000/xmlns/mathml: http://www.w3.org/1998/Math/MathMLsvg: http://www.w3.org/2000/svgxlink: http://www.w3.org/1999/xlink
Custom Aliases
Define custom aliases in the create() configuration using the namespaceAlias key. Use the '@' prefix in method calls to reference them.
Object Conversion Aliases
When converting a JavaScript object to XML, separate the element name and the alias using '@@', and the attribute name and alias using '@att@@'.
const { create } = require('xmlbuilder2');
// 1. Using built-in aliases
const ele1 = create().ele('@xml', 'root').att('@xml', 'att', 'val');
console.log(ele1.toString()); // '<xml:root xml:att=\'val\'/>'
// 2. Using custom aliases
const ele2 = create({ namespaceAlias: { ns: 'ns1' } }).ele('@ns', 'p:root').att('@ns', 'p:att', 'val');
console.log(ele2.toString()); // '<p:root xmlns:p=\'ns1\' p:att=\'val\'/>'
// 3. Using aliases in JS object conversion
const ele3 = create().ele({ 'root@@xml': { '@att@@xml': 'val' }});
console.log(ele3.toString()); // '<xml:root xml:att=\'val\'/>'