### Install libxml2-wasm Package
Source: https://github.com/jameslan/libxml2-wasm/blob/master/README.md
Use npm to install the libxml2-wasm package. This is the first step before importing the library into your project.
```shell
npm i libxml2-wasm
```
--------------------------------
### Install libxml2-wasm
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/tutorial.md
Install the libxml2-wasm npm package using npm.
```shell
npm install libxml2-wasm
```
--------------------------------
### Install Emscripten SDK
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Clone the Emscripten SDK repository, install the latest version, activate it, and source the environment variables. This is necessary for compiling C to WebAssembly.
```bash
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
```
--------------------------------
### Run Benchmark Tests
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/performance.md
Navigate to the benchmark directory, install its dependencies, and execute the tests.
```sh
cd benchmark && npm install
npm test
```
--------------------------------
### Example Exported libxml2 C Function
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
This is an example of a libxml2 C function that can be exported. Functions are listed with an underscore prefix as per Emscripten convention.
```text
_xmlNewDoc
_xmlAddChild
_xmlFreeDoc
...
```
--------------------------------
### Build libxml2-wasm
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/performance.md
Build the library before running benchmarks. Ensure all dependencies are installed first.
```sh
npm ci && npm run build
```
--------------------------------
### Clone and Open Repository
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Clone the repository and open it in VS Code to use the DevContainer. This is the recommended setup for Windows users.
```bash
git clone https://github.com/jameslan/libxml2-wasm.git
code libxml2-wasm
```
--------------------------------
### Example Exported Emscripten Runtime Function
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
This lists Emscripten runtime functions used for memory management that can be exported. Examples include memory accessors and function registration utilities.
```text
HEAP32
UTF8ToString
addFunction
...
```
--------------------------------
### Create an XML Document and Root Node
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Use `XmlDocument.create()` to initialize a new XML document and `createRoot()` to set its root element. This is the starting point for building an XML structure programmatically.
```javascript
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.create();
doc.createRoot('doc');
console.log(doc.toString({ format: false })); //
```
--------------------------------
### Build libxml2-wasm Project
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Clone the project, initialize and update submodules, install Node.js dependencies, and run the build script. This compiles the C code to WebAssembly and TypeScript to JavaScript.
```bash
git clone https://github.com/jameslan/libxml2-wasm.git
cd libxml2-wasm
git submodule update --init --recursive
npm install
npm run build
```
--------------------------------
### Query Nodes with XPath
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Use `get` to retrieve the first matching node and `find` to retrieve all matching nodes using XPath expressions. Ensure to dispose of the document when done.
```js
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.fromString('AmyBob');
console.log(doc.root.get('to').content); // Amy
console.log(doc.root.find('to').map((node) => node.content).join()); // Amy,Bob
doc.dispose();
```
--------------------------------
### Generate Memory Allocation Report
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/mem.md
After running your code with the memory tracker enabled, call `diag.report()` to get a report of objects allocated but not yet disposed.
```typescript
import { diag } from 'libxml2-wasm';
console.log(diag.report());
```
--------------------------------
### Common Development Build Commands
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Run the full build for WebAssembly and TypeScript, or run tests to verify functionality. The `npm test` command also checks coverage and code style.
```bash
npm run build # Full build: WASM + TypeScript
npm test # Verify everything works
```
--------------------------------
### Register Node.js File System Input Providers
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/io.md
Enable libxml2-wasm to read local files using Node.js `fs` module by calling `xmlRegisterFsInputProviders`. This allows parsing files specified by paths or URLs.
```javascript
import { XmlDocument } from 'libxml2-wasm';
import { xmlRegisterFsInputProviders } from 'libxml2-wasm/lib/nodejs.mjs';
xmlRegisterFsInputProviders();
const doc = XmlDocument.fromBuffer(
await fs.readFile('path/to/doc.xml'),
{ url: 'path/to/doc.xml' },
);
doc.dispose();
```
--------------------------------
### Activate Emscripten Environment
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
If you encounter the 'emcc: command not found' error, activate the Emscripten environment by sourcing the provided script. Replace '/path/to/emsdk/' with your actual Emscripten SDK path.
```bash
source /path/to/emsdk/emsdk_env.sh
```
--------------------------------
### Build WASM with Debug Symbols
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Use this command to build the WASM version with debug symbols enabled and optimizations turned off for easier debugging.
```bash
npm run build:debug # Build WASM with debug symbols (-g) and no optimizations (-O0)
```
--------------------------------
### TypeScript Development Workflow
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Use `npm run watch` for automatic recompilation of TypeScript on save during development, or `npm run tsc` for a single compilation. Always run `npm test` to ensure tests, coverage, and linting pass.
```bash
npm run watch # Auto-recompile TypeScript on save
# OR
npm run tsc # Compile TypeScript once
# and always
npm test # Tests + coverage + linting
```
--------------------------------
### XML Input/Output and Callback Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for managing XML input callbacks and general parser/error handling.
```APIDOC
## _xmlCleanupInputCallbacks
### Description
Cleans up the registered input callbacks.
### Method
_xmlCleanupInputCallbacks
### Parameters
None
### Response
None
```
```APIDOC
## _xmlGetLastError
### Description
Retrieves the last error encountered by the parser.
### Method
_xmlGetLastError
### Parameters
None
### Response
- **error** (xmlErrorPtr) - Pointer to the last error structure.
```
```APIDOC
## _xmlRegisterInputCallbacks
### Description
Registers custom input callback functions.
### Method
_xmlRegisterInputCallbacks
### Parameters
- **callbacks** (xmlInputCallbacksPtr) - Pointer to the structure containing input callbacks.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlResetLastError
### Description
Resets the last error state.
### Method
_xmlResetLastError
### Parameters
None
### Response
None
```
```APIDOC
## _xmlSetWinPathEnabled
### Description
Enables or disables Windows path handling.
### Method
_xmlSetWinPathEnabled
### Parameters
- **value** (int) - 1 to enable, 0 to disable.
### Response
None
```
--------------------------------
### Parse XML with Base URL for Relative Paths
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/io.md
Provide a base URL when parsing to correctly resolve relative paths in `` tags. This ensures libxml can calculate the correct URL for included resources.
```javascript
const doc = XmlDocument.fromBuffer(
await fs.readFile('/path/to/doc.xml'),
{ url: 'file:///path/to/doc.xml' },
);
doc.dispose();
```
--------------------------------
### Import and Use XmlDocument from ES Module
Source: https://github.com/jameslan/libxml2-wasm/blob/master/README.md
Import XmlDocument from the libxml2-wasm ES module. Use `fromString` or `fromBuffer` to parse XML. Remember to call `dispose()` to prevent memory leaks.
```javascript
import fs from 'node:fs';
import { XmlDocument } from 'libxml2-wasm';
const doc1 = XmlDocument.fromString('Tove');
const doc2 = XmlDocument.fromBuffer(fs.readFileSync('doc.xml'));
doc1.dispose();
doc2.dispose();
```
--------------------------------
### Compile and Use XPath Expressions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Compile XPath expressions using `XmlXPath.compile` for performance when an XPath is used multiple times. Remember to dispose of the compiled XPath object and documents.
```js
import { XmlDocument, XmlXPath } from 'libxml2-wasm';
const xpath = XmlXPath.compile('/book/title');
const doc1 = XmlDocument.fromString('Harry Potter');
const doc2 = XmlDocument.fromString('Learning XML');
console.log(doc1.get(xpath).content); // Harry Potter
console.log(doc2.get(xpath).content); // Learning XML
doc1.dispose();
doc2.dispose();
xpath.dispose();
```
--------------------------------
### Import and Use XmlDocument from CommonJS
Source: https://github.com/jameslan/libxml2-wasm/blob/master/README.md
Import XmlDocument dynamically from the libxml2-wasm package within a CommonJS environment. Use `fromString` or `fromBuffer` for parsing and ensure `dispose()` is called to manage memory.
```javascript
const fs = require('node:fs');
import('libxml2-wasm').then(({ XmlDocument }) => {
const doc1 = XmlDocument.fromString('Tove');
const doc2 = XmlDocument.fromBuffer(fs.readFileSync('doc.xml'));
doc1.dispose();
doc2.dispose();
});
```
--------------------------------
### Enable Memory Tracker
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/mem.md
Import the `diag` module and configure it to enable memory tracking. This helps in identifying memory leaks by monitoring object allocations.
```typescript
import { diag } from 'libxml2-wasm';
diag.configure({ enabled: true });
```
--------------------------------
### Namespace and Property Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for managing XML namespaces and properties (attributes).
```APIDOC
## _xmlSearchNs
### Description
Searches for a namespace associated with a node.
### Method
_xmlSearchNs
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
- **href** (const xmlChar*) - The namespace URI to search for.
### Response
- **ns** (xmlNsPtr) - Pointer to the found namespace, or NULL if not found.
```
```APIDOC
## _xmlSetNsProp
### Description
Sets or updates a namespaced property on an XML node.
### Method
_xmlSetNsProp
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
- **ns** (xmlNsPtr) - Pointer to the namespace.
- **name** (const xmlChar*) - The name of the property.
- **value** (const xmlChar*) - The value of the property.
### Response
- **prop** (xmlAttrPtr) - Pointer to the created or updated attribute.
```
--------------------------------
### Save XML to File using saveDocSync (Node.js)
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/io.md
Use `saveDocSync` to save an XML document to a file in a Node.js environment. This method is faster than `toString` as it avoids unnecessary UTF-8 to UTF-16 conversions.
```javascript
import fs from 'node:fs';
import { saveDocSync } from 'libxml2-wasm/lib/nodejs.mjs';
const fd = fs.openSync('doc.xml', 'w');
saveDocSync(xml, fd);
```
--------------------------------
### Parse XML from String or Buffer
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/io.md
Use `XmlDocument.fromString` for string input or `XmlDocument.fromBuffer` for buffer input. `fromBuffer` is generally faster due to UTF-8 processing.
```javascript
import fs from 'node:fs';
import { XmlDocument } from 'libxml2-wasm';
const doc1 = XmlDocument.fromString('Tove');
const doc2 = XmlDocument.fromBuffer(fs.readFileSync('doc.xml'));
doc1.dispose();
doc2.dispose();
```
--------------------------------
### Import libxml2-wasm in CommonJS
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/tutorial.md
Use dynamic import for libxml2-wasm when working with CommonJS modules. Ensure `dispose()` is called on the XmlDocument instance to manage memory effectively.
```js
import('libxml2-wasm').then(({ XmlDocument }) => {
const doc = XmlDocument.fromString('Tove');
doc.dispose();
});
```
--------------------------------
### Validate XML against XSD Schema
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Validate an XML document against an XSD schema by first creating an `XmlDocument` from the schema, then initializing an `XsdValidator`, and finally calling the `validate` method. Remember to dispose of the schema, validator, and document.
```js
import fs from 'node:fs';
import { XmlDocument, XsdValidator } from 'libxml2-wasm';
const schema = XmlDocument.fromBuffer(fs.readFileSync('schema.xsd'));
const validator = XsdValidator.fromDoc(schema);
const doc = XmlDocument.fromBuffer(fs.readFileSync('document.xml'));
try {
validator.validate(doc);
} catch (err) {
console.log(err.message);
}
doc.dispose();
validator.dispose();
schema.dispose();
```
--------------------------------
### Import libxml2-wasm in ES Module
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/tutorial.md
Import the XmlDocument class directly when using ES modules. Remember to call `dispose()` on the XmlDocument instance to prevent memory leaks.
```js
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.fromString('Tove');
doc.dispose();
```
--------------------------------
### Memory Management Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for managing memory allocation and deallocation within the WASM environment.
```APIDOC
## _free
### Description
Frees previously allocated memory.
### Method
_free
### Parameters
None
### Response
None
```
```APIDOC
## _malloc
### Description
Allocates a block of memory.
### Method
_malloc
### Parameters
- **size** (int) - The size of the memory block to allocate.
### Response
- **pointer** (void*) - A pointer to the allocated memory block.
```
--------------------------------
### Serialize XML to Compact String
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/io.md
Use `XmlDocument.toString` with the `format: false` option to serialize the XML DOM tree into a compact string without extra whitespace.
```javascript
xml.toString({ format: false });
```
--------------------------------
### XPath Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for evaluating XPath expressions.
```APIDOC
## _xmlXPathCompiledEval
### Description
Evaluates a pre-compiled XPath expression.
### Method
_xmlXPathCompiledEval
### Parameters
- **comp** (xmlXPathCompExprPtr) - Pointer to the compiled XPath expression.
- **ctxt** (xmlXPathContextPtr) - Pointer to the XPath context.
### Response
- **result** (xmlXPathObjectPtr) - Pointer to the XPath result object.
```
```APIDOC
## _xmlXPathCtxtCompile
### Description
Compiles an XPath expression within a given context.
### Method
_xmlXPathCtxtCompile
### Parameters
- **ctxt** (xmlXPathContextPtr) - Pointer to the XPath context.
- **str** (const xmlChar*) - The XPath expression string.
### Response
- **comp** (xmlXPathCompExprPtr) - Pointer to the compiled XPath expression.
```
```APIDOC
## _xmlXPathFreeCompExpr
### Description
Frees a compiled XPath expression.
### Method
_xmlXPathFreeCompExpr
### Parameters
- **comp** (xmlXPathCompExprPtr) - Pointer to the compiled XPath expression to free.
### Response
None
```
```APIDOC
## _xmlXPathFreeContext
### Description
Frees an XPath context.
### Method
_xmlXPathFreeContext
### Parameters
- **ctxt** (xmlXPathContextPtr) - Pointer to the XPath context to free.
### Response
None
```
```APIDOC
## _xmlXPathFreeObject
### Description
Frees an XPath result object.
### Method
_xmlXPathFreeObject
### Parameters
- **obj** (xmlXPathObjectPtr) - Pointer to the XPath result object to free.
### Response
None
```
```APIDOC
## _xmlXPathNewContext
### Description
Creates a new XPath context.
### Method
_xmlXPathNewContext
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
### Response
- **ctxt** (xmlXPathContextPtr) - Pointer to the new XPath context.
```
```APIDOC
## _xmlXPathRegisterNs
### Description
Registers a namespace prefix in an XPath context.
### Method
_xmlXPathRegisterNs
### Parameters
- **ctxt** (xmlXPathContextPtr) - Pointer to the XPath context.
- **prefix** (const xmlChar*) - The namespace prefix.
- **href** (const xmlChar*) - The namespace URI.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlXPathSetContextNode
### Description
Sets the context node for an XPath expression evaluation.
### Method
_xmlXPathSetContextNode
### Parameters
- **node** (xmlNodePtr) - Pointer to the node to set as context.
- **ctxt** (xmlXPathContextPtr) - Pointer to the XPath context.
### Response
None
```
--------------------------------
### Incorrect WASM Module Instance Usage
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Demonstrates an incorrect approach where pointers from a newly created WASM module instance are used with a different, implicitly global instance. This leads to failures because memory spaces are not shared.
```typescript
// -- mynewfeature.mts --
import {xmlSaveDoc} from './libxml2.mjs';
import moduleLoader from "./libxml2raw.mjs";
import {XmlDocument} from './document.mjs';
const libxml2 = await moduleLoader(); // Creates a NEW instance of libxml2 with its own memory space
const ptr = libxml2._malloc(100); // Memory in NEW instance
const doc = XmlDocument.fromString(''); // This uses the "global" instance of libxml2
xmlSaveDoc(ptr, doc._ptr); // FAIL: the ptr and the doc are from different instances
```
--------------------------------
### XML Serialization Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for serializing XML documents to various outputs.
```APIDOC
## _xmlOutputBufferClose
### Description
Closes an output buffer and flushes any remaining data.
### Method
_xmlOutputBufferClose
### Parameters
- **buf** (xmlOutputBufferPtr) - Pointer to the output buffer.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlOutputBufferCreateIO
### Description
Creates an output buffer that writes to an IO function.
### Method
_xmlOutputBufferCreateIO
### Parameters
- **write** (xmlOutputWriteCallback) - The callback function for writing data.
- **close** (xmlOutputCloseCallback) - The callback function for closing the buffer.
- **ctx** (void*) - User data for the callbacks.
- **encoding** (const char*) - The character encoding.
### Response
- **buf** (xmlOutputBufferPtr) - Pointer to the created output buffer.
```
```APIDOC
## _xmlSaveClose
### Description
Closes an XML save context and frees associated resources.
### Method
_xmlSaveClose
### Parameters
- **save_ctx** (xmlSaveCtxtPtr) - Pointer to the save context.
### Response
None
```
```APIDOC
## _xmlSaveDoc
### Description
Saves an XML document to a file or buffer.
### Method
_xmlSaveDoc
### Parameters
- **ctx** (xmlSaveCtxtPtr) - Pointer to the save context.
- **doc** (xmlDocPtr) - Pointer to the XML document to save.
### Response
- **result** (int) - Number of bytes written, or -1 on error.
```
```APIDOC
## _xmlSaveSetIndentString
### Description
Sets the indentation string for saving XML documents.
### Method
_xmlSaveSetIndentString
### Parameters
- **save_ctx** (xmlSaveCtxtPtr) - Pointer to the save context.
- **string** (const xmlChar*) - The indentation string.
### Response
None
```
```APIDOC
## _xmlSaveToIO
### Description
Creates a save context that writes to an IO function.
### Method
_xmlSaveToIO
### Parameters
- **write** (xmlOutputWriteCallback) - The callback function for writing data.
- **close** (xmlOutputCloseCallback) - The callback function for closing the buffer.
- **ctx** (void*) - User data for the callbacks.
- **encoding** (const char*) - The character encoding.
### Response
- **save_ctx** (xmlSaveCtxtPtr) - Pointer to the created save context.
```
```APIDOC
## _xmlSaveTree
### Description
Saves the tree structure of an XML document.
### Method
_xmlSaveTree
### Parameters
- **ctx** (xmlSaveCtxtPtr) - Pointer to the save context.
- **node** (xmlNodePtr) - Pointer to the node to start saving from.
### Response
None
```
--------------------------------
### Dispose a Document Object
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/mem.md
Invoke the `dispose` method on the wrapper object to free the associated WebAssembly memory. This will also free all nodes within the document.
```javascript
doc.dispose();
```
--------------------------------
### Insert Nodes into an XmlElement
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Append or prepend various node types (comments, CDATA, text, elements) to an `XmlElement`. For in-tag nodes like attributes and namespace declarations, use `setAttr` and `addNsDeclaration` respectively.
```javascript
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.fromString('');
const book = doc.root.firstChild;
book.prependComment('all books');
book.appendElement('book');
book.setAttr('order, '1');
doc.toString({ format: false }); // '\n\n'
```
--------------------------------
### Retrieve Attributes Efficiently
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Use `attr()` and `attrs` for direct and efficient retrieval of element attributes, bypassing XPath parsing. Ensure the document is disposed.
```js
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.fromString('');
console.log(doc.root.get('@from').value); // left
console.log(doc.root.attr('from').value); // left
console.log(doc.root.find('@*').map((node) => node.value).join()); // left,right
console.log(doc.root.attrs.map((node) => node.value).join()); // left,right
doc.dispose();
```
--------------------------------
### XML Node Manipulation Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for creating, modifying, and querying XML nodes and documents.
```APIDOC
## _xmlAddChild
### Description
Adds a new child node to a parent node.
### Method
_xmlAddChild
### Parameters
- **parent** (xmlNodePtr) - Pointer to the parent node.
- **cur** (xmlNodePtr) - Pointer to the child node to add.
### Response
- **node** (xmlNodePtr) - Pointer to the added child node.
```
```APIDOC
## _xmlAddNextSibling
### Description
Adds a new node as the next sibling of a given node.
### Method
_xmlAddNextSibling
### Parameters
- **node** (xmlNodePtr) - Pointer to the node to insert after.
- **new__sibling** (xmlNodePtr) - Pointer to the new sibling node.
### Response
- **new__sibling** (xmlNodePtr) - Pointer to the added sibling node.
```
```APIDOC
## _xmlAddPrevSibling
### Description
Adds a new node as the previous sibling of a given node.
### Method
_xmlAddPrevSibling
### Parameters
- **node** (xmlNodePtr) - Pointer to the node to insert before.
- **new__sibling** (xmlNodePtr) - Pointer to the new sibling node.
### Response
- **new__sibling** (xmlNodePtr) - Pointer to the added sibling node.
```
```APIDOC
## _xmlDocGetRootElement
### Description
Retrieves the root element of an XML document.
### Method
_xmlDocGetRootElement
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
### Response
- **root** (xmlNodePtr) - Pointer to the root element.
```
```APIDOC
## _xmlDocSetRootElement
### Description
Sets the root element of an XML document.
### Method
_xmlDocSetRootElement
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
- **root** (xmlNodePtr) - Pointer to the new root element.
### Response
None
```
```APIDOC
## _xmlFreeDoc
### Description
Frees an entire XML document and its contents.
### Method
_xmlFreeDoc
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document to free.
### Response
None
```
```APIDOC
## _xmlFreeDtd
### Description
Frees an XML DTD structure.
### Method
_xmlFreeDtd
### Parameters
- **dtd** (xmlDtdPtr) - Pointer to the DTD to free.
### Response
None
```
```APIDOC
## _xmlFreeNode
### Description
Frees a single XML node and its content.
### Method
_xmlFreeNode
### Parameters
- **node** (xmlNodePtr) - Pointer to the node to free.
### Response
None
```
```APIDOC
## _xmlFreeParserCtxt
### Description
Frees an XML parser context.
### Method
_xmlFreeParserCtxt
### Parameters
- **ctxt** (xmlParserCtxtPtr) - Pointer to the parser context to free.
### Response
None
```
```APIDOC
## _xmlGetIntSubset
### Description
Retrieves the internal DTD subset of an XML document.
### Method
_xmlGetIntSubset
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
### Response
- **subset** (xmlDtdPtr) - Pointer to the internal DTD subset.
```
```APIDOC
## _xmlGetNsList
### Description
Retrieves the list of namespaces associated with an XML node.
### Method
_xmlGetNsList
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
### Response
- **nsList** (xmlNsPtr) - Pointer to the first namespace in the list.
```
```APIDOC
## _xmlHasNsProp
### Description
Checks if an XML node has a property within a specific namespace.
### Method
_xmlHasNsProp
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
- **name** (const xmlChar*) - The name of the property.
- **href** (const xmlChar*) - The namespace URI.
### Response
- **result** (int) - 1 if the property exists, 0 otherwise.
```
```APIDOC
## _xmlNewCDataBlock
### Description
Creates a new XML CDATA section node.
### Method
_xmlNewCDataBlock
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
- **content** (const xmlChar*) - The content of the CDATA section.
- **length** (int) - The length of the content.
### Response
- **node** (xmlNodePtr) - Pointer to the newly created CDATA node.
```
```APIDOC
## _xmlNewDoc
### Description
Creates a new XML document.
### Method
_xmlNewDoc
### Parameters
- **version** (const xmlChar*) - The XML version string (e.g., "1.0").
### Response
- **doc** (xmlDocPtr) - Pointer to the newly created XML document.
```
```APIDOC
## _xmlNewDocComment
### Description
Creates a new XML comment node.
### Method
_xmlNewDocComment
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
- **content** (const xmlChar*) - The content of the comment.
### Response
- **node** (xmlNodePtr) - Pointer to the newly created comment node.
```
```APIDOC
## _xmlNewDocNode
### Description
Creates a new XML node within a document.
### Method
_xmlNewDocNode
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
- **ns** (xmlNsPtr) - Pointer to the namespace.
- **name** (const xmlChar*) - The name of the node.
- **content** (const xmlChar*) - The content of the node.
### Response
- **node** (xmlNodePtr) - Pointer to the newly created node.
```
```APIDOC
## _xmlNewDocTextLen
### Description
Creates a new text node with a specified length.
### Method
_xmlNewDocTextLen
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
- **content** (const xmlChar*) - The text content.
- **len** (int) - The length of the text content.
### Response
- **node** (xmlNodePtr) - Pointer to the newly created text node.
```
```APIDOC
## _xmlNewInputFromMemory
### Description
Creates a new input stream from a memory buffer.
### Method
_xmlNewInputFromMemory
### Parameters
- **buffer** (const char*) - Pointer to the memory buffer.
- **size** (int) - The size of the buffer.
### Response
- **input** (xmlParserInputPtr) - Pointer to the new input stream.
```
```APIDOC
## _xmlNewNode
### Description
Creates a new XML node without associating it with a document.
### Method
_xmlNewNode
### Parameters
- **ns** (xmlNsPtr) - Pointer to the namespace.
- **name** (const xmlChar*) - The name of the node.
### Response
- **node** (xmlNodePtr) - Pointer to the newly created node.
```
```APIDOC
## _xmlNewNs
### Description
Creates a new XML namespace.
### Method
_xmlNewNs
### Parameters
- **node** (xmlNodePtr) - Pointer to the node the namespace is associated with.
- **href** (const xmlChar*) - The namespace URI.
- **prefix** (const xmlChar*) - The namespace prefix.
### Response
- **ns** (xmlNsPtr) - Pointer to the newly created namespace.
```
```APIDOC
## _xmlNewParserCtxt
### Description
Creates a new XML parser context.
### Method
_xmlNewParserCtxt
### Parameters
None
### Response
- **ctxt** (xmlParserCtxtPtr) - Pointer to the new parser context.
```
```APIDOC
## _xmlNewReference
### Description
Creates a new XML entity reference node.
### Method
_xmlNewReference
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
- **name** (const xmlChar*) - The name of the entity.
### Response
- **node** (xmlNodePtr) - Pointer to the newly created reference node.
```
```APIDOC
## _xmlNodeGetContent
### Description
Retrieves the content of an XML node.
### Method
_xmlNodeGetContent
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
### Response
- **content** (xmlChar*) - The content of the node.
```
```APIDOC
## _xmlNodeSetContentLen
### Description
Sets the content of an XML node with a specified length.
### Method
_xmlNodeSetContentLen
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
- **content** (const xmlChar*) - The new content.
- **len** (int) - The length of the new content.
### Response
None
```
```APIDOC
## _xmlRemoveProp
### Description
Removes a property from an XML node.
### Method
_xmlRemoveProp
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
- **name** (const xmlChar*) - The name of the property to remove.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlSetNs
### Description
Associates a namespace with an XML node.
### Method
_xmlSetNs
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
- **ns** (xmlNsPtr) - Pointer to the namespace.
### Response
None
```
```APIDOC
## _xmlSetNsProp
### Description
Sets or updates a namespaced property on an XML node.
### Method
_xmlSetNsProp
### Parameters
- **node** (xmlNodePtr) - Pointer to the XML node.
- **ns** (xmlNsPtr) - Pointer to the namespace.
- **name** (const xmlChar*) - The name of the property.
- **value** (const xmlChar*) - The value of the property.
### Response
- **prop** (xmlAttrPtr) - Pointer to the created or updated attribute.
```
```APIDOC
## _xmlUnlinkNode
### Description
Unlinks a node from its parent and siblings.
### Method
_xmlUnlinkNode
### Parameters
- **node** (xmlNodePtr) - Pointer to the node to unlink.
### Response
None
```
--------------------------------
### XML Parsing and Validation Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for parsing XML documents, DTDs, Relax NG, and Schemas, and for validation.
```APIDOC
## _xmlC14NExecute
### Description
Performs Canonical XML (C14N) serialization.
### Method
_xmlC14NExecute
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
- **exclusive** (int) - Whether to perform exclusive C14N.
- **with_comments** (int) - Whether to include comments.
- **ancestor_ns** (xmlNodePtr) - Ancestor namespace node.
- **inclusive_namespaces** (const xmlChar**) - List of inclusive namespaces.
- **output** (xmlOutputBufferPtr) - Output buffer.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlCtxtParseDtd
### Description
Parses a DTD from a context.
### Method
_xmlCtxtParseDtd
### Parameters
- **ctxt** (xmlParserCtxtPtr) - Pointer to the parser context.
- **internal** (int) - Whether to parse the internal subset.
- **external** (int) - Whether to parse the external subset.
- **bubbleId** (const xmlChar*) - The bubble ID.
- **subsetname** (const xmlChar*) - The name of the subset.
### Response
- **dtd** (xmlDtdPtr) - Pointer to the parsed DTD.
```
```APIDOC
## _xmlCtxtReadMemory
### Description
Parses an XML document from a memory buffer using a context.
### Method
_xmlCtxtReadMemory
### Parameters
- **ctxt** (xmlParserCtxtPtr) - Pointer to the parser context.
- **buffer** (const char*) - The memory buffer containing the XML data.
- **size** (int) - The size of the buffer.
- **encoding** (const char*) - The character encoding of the buffer.
- **options** (int) - Parsing options.
### Response
- **doc** (xmlDocPtr) - Pointer to the parsed XML document.
```
```APIDOC
## _xmlCtxtSetErrorHandler
### Description
Sets the error handler for a parser context.
### Method
_xmlCtxtSetErrorHandler
### Parameters
- **ctxt** (xmlParserCtxtPtr) - Pointer to the parser context.
- **handler** (xmlErrorPtr) - Pointer to the error handler function.
### Response
None
```
```APIDOC
## _xmlCtxtValidateDtd
### Description
Validates a DTD within a parser context.
### Method
_xmlCtxtValidateDtd
### Parameters
- **ctxt** (xmlParserCtxtPtr) - Pointer to the parser context.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlInitParser
### Description
Initializes the XML parser.
### Method
_xmlInitParser
### Parameters
None
### Response
None
```
```APIDOC
## _xmlRelaxNGFree
### Description
Frees a Relax NG schema.
### Method
_xmlRelaxNGFree
### Parameters
- **schema** (xmlRelaxNGPtr) - Pointer to the Relax NG schema to free.
### Response
None
```
```APIDOC
## _xmlRelaxNGFreeParserCtxt
### Description
Frees a Relax NG parser context.
### Method
_xmlRelaxNGFreeParserCtxt
### Parameters
- **ctxt** (xmlRelaxNGParserCtxtPtr) - Pointer to the Relax NG parser context to free.
### Response
None
```
```APIDOC
## _xmlRelaxNGFreeValidCtxt
### Description
Frees a Relax NG validation context.
### Method
_xmlRelaxNGFreeValidCtxt
### Parameters
- **ctxt** (xmlRelaxNGValidCtxtPtr) - Pointer to the Relax NG validation context to free.
### Response
None
```
```APIDOC
## _xmlRelaxNGNewDocParserCtxt
### Description
Creates a new Relax NG parser context for a document.
### Method
_xmlRelaxNGNewDocParserCtxt
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
### Response
- **ctxt** (xmlRelaxNGParserCtxtPtr) - Pointer to the new Relax NG parser context.
```
```APIDOC
## _xmlRelaxNGNewValidCtxt
### Description
Creates a new Relax NG validation context.
### Method
_xmlRelaxNGNewValidCtxt
### Parameters
- **rng** (xmlRelaxNGPtr) - Pointer to the Relax NG schema.
### Response
- **ctxt** (xmlRelaxNGValidCtxtPtr) - Pointer to the new Relax NG validation context.
```
```APIDOC
## _xmlRelaxNGParse
### Description
Parses a Relax NG schema.
### Method
_xmlRelaxNGParse
### Parameters
- **ctxt** (xmlRelaxNGParserCtxtPtr) - Pointer to the Relax NG parser context.
- **URL** (const char*) - The URL of the schema.
### Response
- **schema** (xmlRelaxNGPtr) - Pointer to the parsed Relax NG schema.
```
```APIDOC
## _xmlRelaxNGSetParserStructuredErrors
### Description
Enables or disables structured errors for Relax NG parsing.
### Method
_xmlRelaxNGSetParserStructuredErrors
### Parameters
- **ctxt** (xmlRelaxNGParserCtxtPtr) - Pointer to the Relax NG parser context.
- **errors** (int) - 1 to enable structured errors, 0 to disable.
### Response
None
```
```APIDOC
## _xmlRelaxNGSetValidStructuredErrors
### Description
Enables or disables structured errors for Relax NG validation.
### Method
_xmlRelaxNGSetValidStructuredErrors
### Parameters
- **ctxt** (xmlRelaxNGValidCtxtPtr) - Pointer to the Relax NG validation context.
- **errors** (int) - 1 to enable structured errors, 0 to disable.
### Response
None
```
```APIDOC
## _xmlRelaxNGValidateDoc
### Description
Validates an XML document against a Relax NG schema.
### Method
_xmlRelaxNGValidateDoc
### Parameters
- **ctxt** (xmlRelaxNGValidCtxtPtr) - Pointer to the Relax NG validation context.
- **doc** (xmlDocPtr) - Pointer to the XML document to validate.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlSchemaFree
### Description
Frees an XML Schema.
### Method
_xmlSchemaFree
### Parameters
- **schema** (xmlSchemaPtr) - Pointer to the XML Schema to free.
### Response
None
```
```APIDOC
## _xmlSchemaFreeParserCtxt
### Description
Frees an XML Schema parser context.
### Method
_xmlSchemaFreeParserCtxt
### Parameters
- **ctxt** (xmlSchemaParserCtxtPtr) - Pointer to the XML Schema parser context to free.
### Response
None
```
```APIDOC
## _xmlSchemaFreeValidCtxt
### Description
Frees an XML Schema validation context.
### Method
_xmlSchemaFreeValidCtxt
### Parameters
- **ctxt** (xmlSchemaValidCtxtPtr) - Pointer to the XML Schema validation context to free.
### Response
None
```
```APIDOC
## _xmlSchemaNewDocParserCtxt
### Description
Creates a new XML Schema parser context for a document.
### Method
_xmlSchemaNewDocParserCtxt
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
### Response
- **ctxt** (xmlSchemaParserCtxtPtr) - Pointer to the new XML Schema parser context.
```
```APIDOC
## _xmlSchemaNewValidCtxt
### Description
Creates a new XML Schema validation context.
### Method
_xmlSchemaNewValidCtxt
### Parameters
- **schema** (xmlSchemaPtr) - Pointer to the XML Schema.
### Response
- **ctxt** (xmlSchemaValidCtxtPtr) - Pointer to the new XML Schema validation context.
```
```APIDOC
## _xmlSchemaParse
### Description
Parses an XML Schema.
### Method
_xmlSchemaParse
### Parameters
- **ctxt** (xmlSchemaParserCtxtPtr) - Pointer to the XML Schema parser context.
### Response
- **schema** (xmlSchemaPtr) - Pointer to the parsed XML Schema.
```
```APIDOC
## _xmlSchemaSetParserStructuredErrors
### Description
Enables or disables structured errors for XML Schema parsing.
### Method
_xmlSchemaSetParserStructuredErrors
### Parameters
- **ctxt** (xmlSchemaParserCtxtPtr) - Pointer to the XML Schema parser context.
- **errors** (int) - 1 to enable structured errors, 0 to disable.
### Response
None
```
```APIDOC
## _xmlSchemaSetValidStructuredErrors
### Description
Enables or disables structured errors for XML Schema validation.
### Method
_xmlSchemaSetValidStructuredErrors
### Parameters
- **ctxt** (xmlSchemaValidCtxtPtr) - Pointer to the XML Schema validation context.
- **errors** (int) - 1 to enable structured errors, 0 to disable.
### Response
None
```
```APIDOC
## _xmlSchemaValidateDoc
### Description
Validates an XML document against an XML Schema.
### Method
_xmlSchemaValidateDoc
### Parameters
- **ctxt** (xmlSchemaValidCtxtPtr) - Pointer to the XML Schema validation context.
- **doc** (xmlDocPtr) - Pointer to the XML document to validate.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlSchemaValidateOneElement
### Description
Validates a single element within an XML document against an XML Schema.
### Method
_xmlSchemaValidateOneElement
### Parameters
- **ctxt** (xmlSchemaValidCtxtPtr) - Pointer to the XML Schema validation context.
- **element** (xmlNodePtr) - Pointer to the XML element to validate.
### Response
- **result** (int) - 0 on success, -1 on error.
```
--------------------------------
### Prevent Memory Leaks with `using`
Source: https://github.com/jameslan/libxml2-wasm/blob/master/CONTRIBUTING.md
Always use the `using` keyword or call `.dispose()` on objects that manage resources to prevent memory leaks. The `using` statement ensures automatic disposal.
```typescript
using doc = XmlDocument.fromString('');
// Automatically disposed
```
--------------------------------
### XInclude Functions
Source: https://github.com/jameslan/libxml2-wasm/blob/master/binding/exported-functions.txt
Functions for processing XInclude directives.
```APIDOC
## _xmlXIncludeFreeContext
### Description
Frees an XInclude context.
### Method
_xmlXIncludeFreeContext
### Parameters
- **ctxt** (xmlXIncludeCtxtPtr) - Pointer to the XInclude context to free.
### Response
None
```
```APIDOC
## _xmlXIncludeNewContext
### Description
Creates a new XInclude context.
### Method
_xmlXIncludeNewContext
### Parameters
- **doc** (xmlDocPtr) - Pointer to the XML document.
### Response
- **ctxt** (xmlXIncludeCtxtPtr) - Pointer to the new XInclude context.
```
```APIDOC
## _xmlXIncludeProcessNode
### Description
Processes XInclude directives for a specific node.
### Method
_xmlXIncludeProcessNode
### Parameters
- **ctxt** (xmlXIncludeCtxtPtr) - Pointer to the XInclude context.
- **node** (xmlNodePtr) - Pointer to the node to process.
### Response
- **result** (int) - 0 on success, -1 on error.
```
```APIDOC
## _xmlXIncludeSetErrorHandler
### Description
Sets the error handler for XInclude processing.
### Method
_xmlXIncludeSetErrorHandler
### Parameters
- **ctxt** (xmlXIncludeCtxtPtr) - Pointer to the XInclude context.
- **handler** (xmlErrorPtr) - Pointer to the error handler function.
### Response
None
```
--------------------------------
### Using Declaration for Automatic Disposal
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/mem.md
If your environment supports TypeScript 5.2+ `using` declarations, they can automatically call `[Symbol.dispose]()` when the object goes out of scope.
```typescript
using doc = XmlDocument.fromBuffer(xmlBuffer);
```
--------------------------------
### Modify Node Content
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Update the content of `XmlText`, `XmlComment`, or `XmlCData` nodes using the `content` property. For `XmlAttribute`, use the `value` property.
```javascript
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.fromString('initial');
const textNode = doc.root.firstChild;
textNode.content = 'updated';
console.log(doc.toString({ format: false })); // 'updated'
```
--------------------------------
### Remove a Node from the DOM
Source: https://github.com/jameslan/libxml2-wasm/blob/master/docs/manipulate.md
Remove a node from its parent in the XML DOM tree by calling the `remove()` method on the node itself. This is useful for cleaning up or restructuring the document.
```javascript
import { XmlDocument } from 'libxml2-wasm';
const doc = XmlDocument.fromString('Harry Potter');
doc.root.firstChild.remove();
doc.toString({ format: false }); // '\n'
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.