xast-namespaces
v2.0.0
Published
Attach namespace information to a xast XML element tree.
Downloads
3
Maintainers
Readme
xast-namespaces
Attach namespace information to a xast XML element tree.
This library exposes a function which takes a xast tree and attaches namespace information to each element.
Installation
npm install xast-namespaces
Usage
import { attachNamespaces } from 'xast-namespaces';
const tree = {
type: 'element',
name: 'ns:parent',
attributes: {
'xmlns:ns': 'https://ns.example',
},
children: [
{
type: 'element',
name: 'ns:child',
attributes: {
'ns:attr': 'value',
},
children: [],
},
],
};
const result = attachNamespaces(tree);
assert.deepStrictEqual(result, {
type: 'element',
name: 'ns:parent',
namespace: 'ns',
namespaceURI: 'https://ns.example',
localName: 'parent',
namespaces: {
ns: 'https://ns.example',
},
attributes: {
'xmlns:ns': 'https://ns.example',
},
namespacedAttributes: [
{
type: 'attribute',
name: 'xmlns:ns',
namespace: 'xmlns',
namespaceURI: undefined,
localName: 'ns',
value: 'https://ns.example',
},
],
children: [
{
type: 'element',
name: 'ns:child',
namespace: 'ns',
namespaceURI: 'https://ns.example',
localName: 'child',
namespaces: {
ns: 'https://ns.example',
},
attributes: {
'ns:attr': 'value',
},
namespacedAttributes: [
{
type: 'attribute',
name: 'ns:attr',
namespace: 'ns',
namespaceURI: 'https://ns.example',
localName: 'attr',
value: 'value',
},
],
children: [],
},
],
});
API
attachNamespaces(tree)
Attach XML namespace data to a xast tree.
Options
tree
The document tree to attach namespace data to. If axast
root is passed, its element child will be used.
Returns
A copy of the tree, but with namespace data attached.