Giter VIP home page Giter VIP logo

xml-renderer's Introduction

xml-renderer

A quite simple way of registering rendering functions for XML nodes, effectively an XSL with javascript functions. Works great transforming XML to React components, but writing to a JsonML structure or string is also fine.

To install

npm i xml-renderer --save

To use

Using xml-renderer is two parts; registering the renderer for a given node, and actually rendering that.

Assume a const xr = new XmlRenderer() shared between the following examples.

Registering and rendering

The first parameter to registering a renderer for a node is always an XPath boolean test. The second parameter is a callback that is expected to return the result for any XML node that matches the test. This callback is given an instance of NodeRenderer that allows you to access the actual XML node, or traverse into its children.

// Render every <horizontal-ruler> XML element to an HTML <hr />. This is probably the simplest transformation.
xr.register('self::horizontal-ruler', renderer => <hr key={ renderer.key() } />);

// Render text as text :) This is actually also a pretty simple transformation
xr.register('self::text()', renderer => renderer.getNode().nodeValue);

// Render every XML element that has a "user-id" attribute to a component called "UserBadgeComponent",
//     and pass it the user identifier that we can get from the XML node.
xr.register('self::*[@user-id]', renderer => <UserBadgeComponent
	key={ renderer.key() }
identifier={ renderer.getNode().getAttribute('user-id') }
/>);

Usually you'll start rendering of root nodes, or rendering of child nodes, using NodeRenderer#traverse(). Most of the time you'll actually be rendering a child element, so the traverse() call in a parent element's renderer is very important.

// Render every <paragraph> XML node to a regular <p> HTML element
//     and traverse to render its children.
xr.register('self::paragraph', renderer => <p key={ renderer.key() }>
	{ renderer.traverse() }
</p>);

// Render footnotes as an asterisk, but render them again as list items (+ children) somewhere else.
xr.register('self::footnote', renderer => '*');
xr.mode('footnote-render-mode').register('self::footnote', renderer => <li key={ renderer.key() }>
	{ renderer.traverse() }
</li>);

// Render a <webpage> XML node with a second traversal in a different mode for all its <footnote> descendants
//     in an ordered list under a horizontal ruler.
xr.register('self::webpage', renderer => <div key={ renderer.key() }>
	{ renderer.traverse() }
	<hr />
	<ol>
		{ renderer.traverse('.//footnote', 'footnote-render-mode') }
	</ol>
</div>);

To finally engage all of the above for an XML string that you've loaded from anywhere, use a DOMParser to create a DOM out of it and start xr for that.

// Load an XML string from anywhere
const xmlString = `
	<webpage>
		<author user-id="wvbe" />
		<paragraph>The preceding element, "author", was not specifically mentioned in this example yet, but it matches the XPath test for 'self::*[@user-id]'.</paragraph>
		<horizontal-ruler />
		<paragraph>Footnotes will be rendered as an asterisk<footnote><paragraph>But the full footnote is rendered at the bottom of a page</paragraph></footnote></paragraph>
	</webpage>
`;

// Create a DOM
const xmlDom = new window.DOMParser().parseToString(xmlString, 'application/xml');

// Render that baby
ReactDOM.render(
	<div>{ xr.node(xmlDom).traverse() }</div>,
	document.getElementById('root')
);

This should yield a DOM like this:

<div>
	<UserBadgeComponent identifier="wvbe" />
	<p>The preceding element, "author", was not specifically mentioned in this example yet, but it matches the XPath test for 'self::*[@user-id]'.</p>
	<hr />
	<p>Footnotes will be rendered as an asterisk*</p>
	<hr />
	<ol>
		<li>
			<p>But the full footnote is rendered at the bottom of a page</p>
		</li>
	</ol>
</div>

The full code for this example is in example/simple.js.

xml-renderer's People

Contributors

wvbe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.