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

@xmpp/xml

v0.13.1

Published

XMPP XML for JavaScript

Downloads

54,491

Readme

xml

Install

Note, if you're using @xmpp/client or @xmpp/component, you don't need to install @xmpp/xml.

npm install @xmpp/xml

const xml = require("@xmpp/xml");
const { xml } = require("@xmpp/client");
const { xml } = require("@xmpp/component");

Writing

There's 2 methods for writing XML with xmpp.js

factory

const xml = require("@xmpp/xml");

const recipient = "[email protected]";
const days = ["Monday", "Tuesday", "Wednesday"];
const message = xml(
  "message",
  { to: recipient },
  xml("body", {}, 1 + 2),
  xml(
    "days",
    {},
    days.map((day, idx) => xml("day", { idx }, day)),
  ),
);

If the second argument passed to xml is a string instead of an object, it will be set as the xmlns attribute.

// both are equivalent
xml("time", "urn:xmpp:time");
xml("time", { xmlns: "urn:xmpp:time" });

JSX

/** @jsx xml */

const xml = require("@xmpp/xml");

const recipient = "[email protected]";
const days = ["Monday", "Tuesday"];
const message = (
  <message to={recipient}>
    <body>{1 + 2}</body>
    <days>
      {days.map((day, idx) => (
        <day idx={idx}>${day}</day>
      ))}
    </days>
  </message>
);

Requires a preprocessor such as TypeScript or Babel with @babel/plugin-transform-react-jsx.

Reading

attributes

The attrs property is an object that holds xml attributes of the element.

message.attrs.to; // [email protected]

text

Returns the text value of an element

message.getChild("body").text(); // '3'

getChild

Get child element by name.

message.getChild("body").toString(); // '<body>3</body>'

getChildText

Get child element text value.

message.getChildText("body"); // '3'

getChildren

Get children elements by name.

message.getChild("days").getChildren("day"); // [...]

Since getChildren returns an array, you can use JavaScript array methods such as filter and find to build more complex queries.

const days = message.getChild("days").getChildren("day");

// Find Monday element
days.find((day) => day.text() === "Monday");
days.find((day) => day.attrs.idx === 0);

// Find all days after Tuesday
days.filter((day) => day.attrs.idx > 2);

parent

You can get the parent node using the parent property.

console.log(message.getChild("days").parent === message);

root

You can get the root node using the root method.

console.log(message.getChild("days").root() === message);

Editing

attributes

The attrs property is an object that holds xml attributes of the element.

message.attrs.type = "chat";
Object.assign(message.attrs, { type: "chat" });

text

Set the text value of an element

message.getChild("body").text("Hello world");

append

Adds text or element nodes to the last position. Returns the parent.

message.append(xml("foo"));
message.append("bar");
message.append(days.map((day) => xml("day", {}, day)));
// <message>
//   ...
//   <foo/>
//   bar
//   <day>Monday</day>
//   <day>Tuesday</day>
// </message>

prepend

Adds text or element nodes to the first position. Returns the parent.

message.prepend(xml("foo"));
message.prepend("bar");
message.prepend(days.map((day) => xml("day", {}, day)));
// <message>
//   <day>Tuesday</day>
//   <day>Monday</day>
//   bar
//   <foo/>
//   ...
// </message>

remove

Removes a child element.

const body = message.getChild("body");
message.remove(body);

JSON

You can embed JSON anywhere but it is recommended to use appropriate semantic.

/** @jsx xml */

// write
message.append(
  <myevent xmlns="xmpp:example.org">
    <json xmlns="urn:xmpp:json:0">{JSON.stringify(days)}</json>
  </myevent>,
);

// read
JSON.parse(
  message
    .getChild("myevent", "xmpp:example.org")
    .getChildText("json", "urn:xmpp:json:0"),
);

See also JSON Containers and Simple JSON Messaging.

Parsing XML strings

@xmpp/xml include a function to parse XML strings.

⚠ Use with care. Untrusted input or substitutions can result in invalid XML and side effects.

const { escapeXML, escapeXMLText };
const parse = require("@xmpp/xml/lib/parse");

const ctx = parse("<message><body>hello world</body></message>");
ctx.getChildText("body"); // hello world

If you must use with untrusted input, escape it with escapeXML and escapeXMLText.

const { escapeXML, escapeXMLText } = require("@xmpp/xml");
const parse = require("@xmpp/xml/lib/parse");

const message = parse(`
  <message to="${escapeXML(to)}">
    <body>${escapeXMLText(body)}</body>
  </message>,
`);