npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

xsd2js

v1.0.9

Published

A simple XSD to JS file generator, with serialization/unserialization

Readme

XSD to JavaScript Code Generator

This command-line tool generates JavaScript classes from an XML Schema Definition (XSD) file. It supports customization via template files and several options to control code generation.

📦 Installation

Clone the repository and install dependencies (if any):

git clone https://github.com/rberaud/xsd2js
cd xsd2js
npm install

you can also install the tool from npm

npm install xsd2js -g

🚀 Usage

xsd2js -i <input.xsd> -o <output_path> [options]

Please note that the xsd2js shortcut is bound to node ./src/main.js

📚 Options

| Option | Alias | Type | Required | Default | Description | | -------------------------- | ----- | --------- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------- | | --input | -i | string | ✅ | | Path to the input XSD file. | | --output | -o | string | ✅ | | Output path (file or directory). | | --multiple-files | -m | boolean | ❌ | false | Generate a separate file for each class. | | --base | -b | string | ❌ | | Path to a custom Base.js file used as base class. | | --XSD-type | | boolean | ❌ | false | Track the original XSD type for each field. | | --XML-type | | boolean | ❌ | false | Track whether a field is an XML attribute. | | --transparent-attributes | | boolean | ❌ | false | Expose XML attributes as normal properties (no @_ prefix). | | --template-file | | string | ❌ | | Path to a custom template file for class generation. | | --template-tag-body | | string | ❌ | | Tag in the template used for constructor body (e.g., "tag-body"). | | --template-tag-meta | | string | ❌ | | Tag in the template used for metadata section (e.g., "tag-meta"). | | --template-tag-header | | string | ❌ | | Tag in the template used for header (e.g., "tag-header"). | | --generate-accessors | | boolean | ❌ | true | When true generated classes expose properties through getters/setters backed by hidden fields (useful to prevent direct mutation). | | --accessors-notification | | boolean | ❌ | true | When true (and when --generate-accessors is enabled) setters will emit change notifications to subscribers on the instance. |

🛠 Example

Generate a single file from an XSD:

node index.js -i schema.xsd -o output.js

Generate multiple class files:

node index.js -i schema.xsd -o ./output-directory -m

Generate classes using accessor-backed properties and notifications (both enabled by default):

node src/main.js -i schema.xsd -o ./generated -m --generate-accessors --accessors-notification

Disable accessor notifications while keeping accessors enabled:

node src/main.js -i schema.xsd -o ./generated -m --generate-accessors --no-accessors-notification

Use a custom template:

node index.js -i schema.xsd -o ./output -m \
  --template-file template.txt \
  --template-tag-body "%%BODY%%" \
  --template-tag-meta "%%META%%" \
  --template-tag-header "%%HEADER%%"

Use a custom base class:

xsd2js -i schema.xsd -o ./output -b ./Base.js

📎 How to use the generated files

Unless you provide your own template file using options like --template-file, you can reuse the generated files with the ./template/base.js file provided. This class is a root class for every generated class, and it provides basic marshalling/unmarshalling features to/from XML

📎 Notes

  • XML attributes are prefixed with @_ by default unless --transparent-attributes is enabled.
  • Templates should contain placeholders (template-tag-*) which will be replaced by generated code.
  • If you use --XSD-type or --XML-type, the metadata will include this information for introspection or validation purposes.

🔔 Notifications (accessors)

When generated with --generate-accessors (default) and --accessors-notification (default), each generated class instance exposes a subscription API implemented on Base.

  • const token = instance.subscribe(listener) — subscribe to events on this instance. listener can be either:
    • a function that will be called with an event object, or
    • an object that implements onPropertyChange(event) (this allows adding other future event handler methods on the same listener object).
  • instance.unsubscribe(token) — remove the subscription.

For property changes the event object has the shape: { target, property, oldValue, newValue }. The template exposes this via a method named _notifyPropertyChanged (called by generated setters).

Examples:

Function listener:

import { UADataType } from "./generated/UADataType.js";

const n = new UADataType({
  /* initial data */
});

const token = n.subscribe(({ target, property, oldValue, newValue }) => {
  console.log(
    `Property ${property} changed from`,
    oldValue,
    "to",
    newValue,
    "on",
    target
  );
});

n.Definition = {
  /* new value */
};

// Unsubscribe when done
n.unsubscribe(token);

Object listener:

const listener = {
  onPropertyChange({ target, property, oldValue, newValue }) {
    console.log(
      "object listener",
      property,
      oldValue,
      newValue,
      target.constructor.name
    );
  },
};

const token2 = n.subscribe(listener);
n.Purpose = "some purpose";
n.unsubscribe(token2);

📤 Output

Depending on your options, the output will be:

  • A single .js file with all generated classes.
  • Multiple .js files in a directory (when --multiple-files is enabled).
  • Each class will be based on a default or custom Base.js class, if provided.

🛠 Tests

cd examples
xsd2js --i ./UANodeSet.xsd -o ./references --m --transparent-attributes --XSD-type--XML-type

A set of OPC-UA js files should be generated in the examples/references folder.