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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xml-tools/simple-schema

v3.0.5

Published

XML Simple Schema

Downloads

2,710

Readme

npm (scoped)

@xml-tools/simple-schema

This package includes three elements:

  • Data structure definition for a basic XML schema represented as a JavaScript object literal.
  • Validation logic to inspect a given XML conforms to a Schema.
  • Content Assist logic that provides suggestions using the Schema information.

Installation

With npm:

  • npm install @xml-tools/simple-schema

With Yarn

  • yarn add @xml-tools/simple-schema

Usage

Please see the TypeScript Definitions for full API details.

  • Defining a Schema:

    const schema = {
      name: "people",
      required: true,
      cardinality: "single",
      attributes: {},
      elements: {
        person: {
          name: "person",
          required: false,
          cardinality: "many",
          attributes: {
            eyeColor: {
              key: "eyeColor",
              required: false,
              value: ["grey", "blue", "green", "red"],
            },
          },
          elements: {
            name: {
              cardinality: "single",
              required: true,
              name: "name",
              attributes: {},
              elements: {},
            },
          },
        },
      },
    };
  • A Simple XML Text

    // A Sample XML Text
    const xmlText = `<people>
                           <person eyeColor="violet">
                             <name>Daenerys Targaryen</name>
                           </person>
                        </people>`;
  • Performing Validations

    const { parse } = require("@xml-tools/parser");
    const { buildAst } = require("@xml-tools/ast");
    const { validate } = require("@xml-tools/validation");
    const { getSchemaValidators } = require("@xml-tools/simple-schema");
    
    const { cst, tokenVector } = parse(xmlText);
    const xmlDoc = buildAst(cst, tokenVector);
    const schemaValidators = getSchemaValidators(schema);
    const issues = validate({
      doc: xmlDoc,
      validators: {
        attribute: [schemaValidators.attribute],
        element: [schemaValidators.element],
      },
    });
    
    console.log(issues[0].msg); // Expecting one of <grey,blue,green,red> but found <violet>
    console.log(issues[0].position); // { startOffset: 46, endOffset: 53 }
  • Using Content Assist APIs

    const { getSuggestions } = require("@xml-tools/content-assist");
    const { getSchemaSuggestionsProviders } = require("@xml-tools/simple-schema");
    
    const schemaSuggestionsProviders = getSchemaSuggestionsProviders(schema);
    
    const suggestions = getSuggestions({
      text: xmlText,
      offset: 47, // within the eyeColor quoted value
      providers: {
        attributeValue: [
          schemaSuggestionsProviders.schemaAttributeValueCompletion,
        ],
        attributeName: [schemaSuggestionsProviders.schemaAttributeNameCompletion],
        elementName: [schemaSuggestionsProviders.schemaElementNameCompletion],
      },
    });
    
    console.log(suggestions[0]); // { text: 'grey', label: 'grey' }
    console.log(suggestions[1]); // { text: 'blue', label: 'blue' }

Support

Please open issues on github.

Contributing

See CONTRIBUTING.md.