Giter Club home page Giter Club logo

grunt-xsltproc's Introduction

grunt-xsltproc

Apply XSLT stylesheets to XML documents.

Getting Started

This plugin requires Grunt >=0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-xsltproc --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks("grunt-xsltproc");

The "xsltproc" task

Overview

Run this task with the grunt xsltproc command.

xsltproc

xsltproc is an open-source command line tool for applying XSLT stylesheets to XML documents.

This task requires you to have xsltproc installed. If you're on OS X or Linux you probably already have it installed. You can check by running xsltproc -version in your terminal.

Setup

First, ensure you have xsltproc installed. Then, in your project's Gruntfile, add a section named xsltproc to the data object passed into grunt.initConfig().

grunt.initConfig({
  xsltproc: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
});

Options

options.stylesheet

Type: String or false

Path to the XSLT stylesheet to apply to the files. If options.stylesheet evaluates to false, xsltproc will fallback to stylesheets linked from the target document.

options.params

Type: Object

Pass an object of parameter name/value pairs to the stylesheet. Parameters are passed directly to the xsltproc call with the --param flag.

options.stringparams

Type: Object

Pass an object of string parameter name/value pairs to the stylesheet. Parameters are passed directly to the xsltproc call with the --stringparam flag.

options.xinclude

Type: Boolean

Process the input document using the XInclude specification. More details on this can be found in the XInclude specification.

options.xincludestyle

Type: Boolean

Process the stylesheet using the XInclude specification.

options.novalid

Type: Boolean

Process the input document skipping the DTD loading phase.

options.filepath

Type: Boolean

Pass the grunt filepath as stringparam to the XSLT.

options.html

Type: Boolean

The input document is(are) an HTML file(s).

Usage Examples

Single file

grunt.initConfig({
  xsltproc: {
    options: {
      stylesheet: "test/fixtures/compiler.xsl",
    },
    compile: {
      files: {
        "tmp/compile.html": ["test/fixtures/compile.xml"],
      },
    },
  },
});

Directory of files

grunt.initConfig({
  xsltproc: {
    options: {
      stylesheet: "test/fixtures/compiler.xsl",
    },
    compile: {
      files: [
        {
          expand: true,
          cwd: "test/fixtures",
          src: "*.xml",
          dest: "tmp",
          ext: ".html",
        },
      ],
    },
  },
});

Passing parameters

grunt.initConfig({
  xsltproc: {
    options: {
      stylesheet: "test/fixtures/compiler.xsl",
      params: {
        fav: '/catalogue/album[@id="3"]/title',
      },
    },
    compile: {
      files: {
        "tmp/compile.html": ["test/fixtures/compile.xml"],
      },
    },
  },
});

Passing string parameters

grunt.initConfig({
  xsltproc: {
    options: {
      stylesheet: "test/fixtures/compiler.xsl",
      stringparams: {
        title: "My Music",
      },
    },
    compile: {
      files: {
        "tmp/compile.html": ["test/fixtures/compile.xml"],
      },
    },
  },
});

XInclude

Process the input document using the XInclude specification.

grunt.initConfig({
  xsltproc: {
    options: {
      stylesheet: "test/fixtures/compiler.xsl",
      xinclude: true,
    },
    compile: {
      files: {
        "tmp/compile.html": ["test/fixtures/compile.xml"],
      },
    },
  },
});

Process the stylesheet using the XInclude specification.

grunt.initConfig({
  xsltproc: {
    options: {
      xincludestyle: true,
    },
    compile: {
      options: {
        stylesheet: "test/fixtures/compiler.xsl",
      },
      files: {
        "tmp/compile.html": ["test/fixtures/compile.xml"],
      },
    },
  },
});

filepath

Process the input documents and using the filepath xsl:param.

grunt.initConfig({
  xsltproc: {
    compile: {
      options: {
        stylesheet: 'test/fixtures/template_filepath.xsl'
        filepath: true
      },
      files: [{
        expand: true,
        cwd: 'test/fixtures',
        src: '*.xml',
        dest: 'tmp',
        ext: '.html'
      }]
    }
  }
})
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>

  <xsl:param name="filepath"/>

  <xsl:template match="/">
    ...
  </xsl:template>
</xsl:stylesheet>

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

grunt-xsltproc's People

Contributors

andybluntish avatar bpaulin avatar dellermann avatar dsine-de avatar pdehaan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-xsltproc's Issues

Installation failing (npm install)

I currently get a 404 error when trying to install grunt-xsltproc using npm:

npm http GET https://registry.npmjs.org/grunt-xsltproc/-/grunt-xsltproc-0.3.0.tgz
npm http 404 https://registry.npmjs.org/grunt-xsltproc/-/grunt-xsltproc-0.3.0.tgz
npm ERR! fetch failed https://registry.npmjs.org/grunt-xsltproc/-/grunt-xsltproc-0.3.0.tgz
npm ERR! Error: 404 Not Found
...

failed to load external entity "xsltproc"

Tested on Windows 7 x64, and Linux (CentOS 6.5) x64, with xsltproc installed to my PATH:

Running "xsltproc:svg2symbol" (xsltproc) task
warning: failed to load external entity "xsltproc"
cannot parse xsltproc
Warning: warning: failed to load external entity "xsltproc"
cannot parse xsltproc Use --force to continue.

The bare command xsltproc foo.xslt bar.xml works fine, but not the Grunt task. My config:

xsltproc: {
  options: {
    stylesheet: '<%= config.app %>/assets/svg2symbol.xslt'
  },
  svg2symbol: {
    files: [{
      expand: true,
      cwd: '<%= config.app %>/assets/img',
      src: '*.svg',
      dest: '<%= build_dir %>/assets/img',
      ext: '.symbol.svg'
    }]
  }
},

Should this work on windows?

I installed xsltproc and all dependencies (Windows 7) and edited the PATH variable.
I can run "xsltproc" on the command line from anywhere.

But when I try to run the Grunt task, I get this error:

Running "xsltproc:compile" (xsltproc) task
Warning: You need to have xsltproc installed and in your PATH for this task to work. Use --force to continue.

Aborted due to warnings.

The Grunt task configuration:

...
        xsltproc: {
            options: {
                stylesheet: 'xsl/myxsl.xsl'
            },
            compile: {
                files: {
                    'result.html': ['xml/myxml.xml']
                }
            }
        }
...

Doesn't output destination file

Config:

xsltproc: {
  options: {
  stylesheet: '<%= config.app %>/assets/svg2symbol.xslt'
},
files: {
  '<%= config.app %>/assets/img/icon.symbol.svg': [
    '<%= config.app %>/assets/img/icon.svg'
  ]
}

When I run grunt, it silently fails, and the output file is not created.

Support multiple targets

Hi Andy,

is it possible to add support for target specific stylesheets, so you can have multiple XML and XSL files?

Something like this:

xsltproc: {
    options: {
        xinclude: true
    },
    target1: {
        stylesheet: 'stylesheet1.xsl',
        compile: {
            files: {
                'result1.html': ['myxml.xml']
            }
        }
    },
    target2: {
        stylesheet: 'stylesheet2.xsl',
        compile: {
            files: {
                'result2.html': ['../resources/xml/registration.xml']
            }
        }
    }
    ...

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.