JSLT

HTML

<div id="examples"></div>

JavaScript

/*
  JSON transformations, revisited

  Cyril Jandia, 2016-2017

  Public Domain.

  NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
 */
 
/*
  Cf. https://www.w3.org/TR/xslt#section-Document-Example
*/
var D1document = {
    type: "document", title: [ "(Document Title)" ],
    "": [
      { type: "chapter", title: [ "Chapter Title" ],
        "": [
        { type: "section", title: [ "Section Title" ],
          "": [
            { type: "para", "": [ "This is a test." ] },
            { type: "note", "": [ "This is a note." ] }
        ] },
        { type: "section", title: [ "Another Section Title" ],
          "": [
            { type: "para", "": [ "This is ", { emph: "another" }, " test." ] },
            { type: "note", "": [ "This is another note." ] }
        ] }
      ] }
    ] };

document.getElementById("examples").innerHTML += "<p>From:</p>";
document.getElementById("examples").innerHTML += "<xmp>D1document = " + JSON.stringify(D1document, null, 2) + "</xmp>";

var D1toHTML = { "": [
  [ [ function(node) { return node.type === "document"; } ],
    function(root) {
      return T("<html>\r\n\
  <head>\r\n\
    <title>\r\n\
      {title}\r\n").from(root) + T("\
    </title>\r\n\
  </head>\r\n\
  <body>\r\n\
{*}").from(T(this).from(root[""])) + "\
  </body>\r\n\
</html>";
    }
  ],
  [ [ function(node) { return node.type === "chapter"; } ],
    function(chapter) {
      return T("    <h2>{title}</h2>\r\n").from(chapter) + T("{*}").from(T(this).from(chapter[""]));
    }
  ],
  [ [ function(node) { return node.type === "section"; } ],
    function(section) {
      return T("    <h3>{title}</h3>\r\n").from(section) + T("{*}").from(T(this).from(section[""]));
    }
  ],
  [ [ function(node) { return node.type === "para"; } ],
    function(para) {
      return T("    <p>{*}</p>\r\n").from(T(this).from(para[""]));
    }
  ],
  [ [ function(node) { return node.type === "note"; } ],
    function(note) {
      return T('    <p class="note"><b>NOTE:...