xast-namespaces
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

xast-namespaces

Attach namespace information to a xast XML element tree.

build status codecov npm prettier

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 a xast root is passed, its element child will be used.

Returns

A copy of the tree, but with namespace data attached.

License

MIT © Remco Haszing

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 2.0.0
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i xast-namespaces

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

11.8 kB

Total Files

5

Last publish

Collaborators

  • remcohaszing