Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/remcohaszing/xast-namespaces
Attach namespace information to a xast XML element tree.
https://github.com/remcohaszing/xast-namespaces
javascript unist xast xml
Last synced: 7 days ago
JSON representation
Attach namespace information to a xast XML element tree.
- Host: GitHub
- URL: https://github.com/remcohaszing/xast-namespaces
- Owner: remcohaszing
- License: mit
- Created: 2021-01-13T20:24:28.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-25T12:58:53.000Z (about 2 years ago)
- Last Synced: 2024-12-07T05:24:22.317Z (27 days ago)
- Topics: javascript, unist, xast, xml
- Language: TypeScript
- Homepage:
- Size: 467 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# xast-namespaces
> Attach namespace information to a xast XML element tree.
[![build status](https://github.com/remcohaszing/xast-namespaces/workflows/ci/badge.svg)](https://github.com/remcohaszing/xast-namespaces/actions)
[![codecov](https://codecov.io/gh/remcohaszing/xast-namespaces/branch/main/graph/badge.svg)](https://codecov.io/gh/remcohaszing/xast-namespaces)
[![npm](https://img.shields.io/npm/v/xast-namespaces)](https://www.npmjs.com/package/xast-namespaces)
[![prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io)This library exposes a function which takes a [xast](https://github.com/syntax-tree/xast) tree and
attaches namespace information to each element.## Installation
```sh
npm install xast-namespaces
```## Usage
```js
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](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)