Library to complement libxmljs and especially make namespaces easier to deal with.

@dreptin dreptin authored on 30 Apr 2021
src fix: remade multiple overloads for encoding() and root() because the optional argument way didn't work 2 years ago
test Initial commit 2 years ago
.babelrc chore: switched to babel parser to make eslint support ES6 classes better 2 years ago
.editorconfig Initial commit 2 years ago
.eslintrc.json chore: switched to babel parser to make eslint support ES6 classes better 2 years ago
.gitignore Initial commit 2 years ago
LICENSE Initial commit 2 years ago
README.md docs: fixed the example in README.md to make it work 2 years ago
index.js feat: created a Document class to be exactly like the one from libxmljs with a few tweaks 2 years ago
package-lock.json chore: switched to babel parser to make eslint support ES6 classes better 2 years ago
package.json chore: switched to babel parser to make eslint support ES6 classes better 2 years ago
README.md

libxmljs-extra

Library of functions to complement libxmljs.

const libxmljs = require('libxmljs');
const extras = require('libxmljs-extra');

const xml = `<?xml version="1.0" encoding="UTF-8"?>
            <root xmlns="http://www.my-namespace.org/">
              <child>
                <grandchild>Hello</grandchild>
              </child>
              <child>
                <grandchild>World</grandchild>
              </child>
            </root>`;

const xmlDoc = libxmljs.parseXml(xml);

// Sets the global namespace of the document
extras.setNamespace(xmlDoc, 'namespace', 'http://www.my-namespace.org/');

const grandchildren = extras.find(xmlDoc, '//grandchild');

grandchildren.forEach((grandchild) => console.log(grandchild.text())); // Prints "Hello\nWorld"

console.log(extras.count(xmlDoc, '//grandchild')); // Prints "2"

Docs

count(xmlDoc, xpath)

Counts the amount of results of the provided XPath.

args
xmlDoc The instance of Document representating the XML document.
xpath The XPath to count in xmlDoc.

returns
The amount of results.

find(xmlDoc, xpath)

Finds the result of the provided XPath.

args
xmlDoc The instance of Document representating the XML document.
xpath The XPath to get in xmlDoc.

returns
The found Element or Elements.

setNamespace(xmlDoc, alias, url)

Sets the namespace to use for the specified Document.

args
xmlDoc The Document instance to set the namespace of.
alias The namespace alias.
url The namespace URL.

returns
void