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

jsx-xml

v0.2.3

Published

Generate xml string from jsx

Downloads

1,957

Readme

jsx-xml

Generate xml string from jsx

Build Status codecov

install

Add jsx-xml packages

yarn add jsx-xml 

or

npm install jsx-xml --save 

You need to add the babel-plugin-transform-react-jsx package if not already installed.

usage

/** @jsx JSXXML */
import {render, JSXXML} from 'jsx-xml'

const xml = render(<test x="3">1 + {2} = {3}</test>) // jsx input
console.log(xml) // xml output: <?xml version="1.0"?><test x="3">1 + 2 = 3</test> 

API

You can import the following functions from the jsx-xml package:

import {render, JSXXML, CData, Comment, Fragment, Raw} from 'jsx-xml'

render

render(jsx, options) converts jsx (JSXXML output) to xml string.

options:

My favorite options is:

const options = {createOptions: {headless: true}, endOptions: {pretty: true}}

JSXXML

JSXXML(type, attr, ...children) is a jsx pragma.

  • type: string or function

If type is a string, it will be considered and used as an xml tag. In case the jsx tag is in lowercase, it will be considered as a string in compile time. (e.g. <test />)

If type is a function, the output of the JSXXML function will equal that of type when called with parameters attr and children.

If you need a non-lowercase xml tag, you can create a variable named Capitalized and use it in your jsx script.

Examples:

The JSX code:

/** @jsx JSXXML */

const Banner = ({color, size, children}) => { /* ... */ };

<Banner color="blue" size={2}>
  Click Star
</Banner>

compiles into:

/** @jsx JSXXML */

const Banner = ({ color, size, children }) => {/* ... */};

JSXXML(
  Banner,
  { color: "blue", size: 2 },
  "Click Star"
);

You can also use the self-closing form of the tag if there are no children:

/** @jsx JSXXML */

<user username="Bob" />

compiles into:

/** @jsx JSXXML */

JSXXML("user", { username: "Bob" });

CData

This tag allows you to add cdata to your xml output.

/** @jsx JSXXML */
import { render, JSXXML, CData } from 'jsx-xml'

const xml = render(<test x="3"><CData>this is an example {'cdata'}</CData></test>, options)

output:

<test x="3">
  <![CDATA[this is an example cdata]]>
</test>

Comment

This tag allows you to add comments to your xml output.

/** @jsx JSXXML */
import { render, JSXXML, Comment } from 'jsx-xml'

const xml = render(<test><Comment>1 + {2} = {3}</Comment></test>, options)

output

<test>
  <!-- 1 + 2 = 3 -->
</test>

Fragment

This tag allows you to return more than one element. Fragment is useful in functions.

import { render, JSXXML, Fragment } from 'jsx-xml'

const Items = () => <Fragment>
  <item value="1" />
  <item value="2" />
</Fragment>

const xml = render(<items>
  <Items />
</items>, options)

output:

<items>
  <item value="1"/>
  <item value="2"/>
</items>

Raw

This tag allows you to add data to your xml without escaping.

/** @jsx JSXXML */
import { render, JSXXML, Raw } from 'jsx-xml'

const data = 'this text contain < and > and & and ;'
const xml = render(<items>
  <x>{data}</x>
  <y><Raw>{data}</Raw></y>
</items>, options)

output:

<items>
  <x>this text contain &lt; and &gt; and &amp; and ;</x>
  <y>this text contain < and > and & and ;</y>
</items>

How to config babel for jsx-xml

You can include /** @jsx JSXXML */ at the first line of your file or pass it as pragma param to the transform-react-jsx plugin present in .babelrc:

{
  "plugins": [
    [
      "transform-react-jsx",
      {
        "pragma": "JSXXML"
      }
    ]
  ]
}

You can also use babel-plugin-jsx-pragmatic to avoid importing JSXXML in every page.

{
  "plugins": [
  [
    "jsx-pragmatic",
      {
        "module": "jsx-xml",
        "import": "JSXXML",
        "export": "JSXXML"
      }
    ]
  ]
}