Skip to content
Getting Started

Getting Started

This page walks you through running goxslt for the first time, both from the command line and from a Go program.

Install

goxslt is a Go module. With Go 1.24+ installed:

go install github.com/speedata/goxslt/cmd/goxslt@latest

This builds the goxslt binary into your $GOPATH/bin (or $GOBIN).

A first stylesheet

Create a small input document books.xml:

<library>
  <book>
    <title>The Hobbit</title>
    <author>Tolkien</author>
  </book>
  <book>
    <title>1984</title>
    <author>Orwell</author>
  </book>
</library>

And a stylesheet books.xsl that turns it into HTML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <html>
      <body>
        <h1>Library</h1>
        <ul>
          <xsl:apply-templates select="library/book"/>
        </ul>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="book">
    <li>
      <xsl:value-of select="title"/>
      <xsl:text></xsl:text>
      <xsl:value-of select="author"/>
    </li>
  </xsl:template>
</xsl:stylesheet>

Run the transformation:

goxslt -s books.xml -t books.xsl

The result is written to standard output. Use -o output.html to write to a file.

Passing parameters

Top-level xsl:param declarations can be set from the command line as key=value pairs after the file arguments:

goxslt -s books.xml -t books.xsl title=Library author-filter=Orwell

Inside the stylesheet:

<xsl:param name="title" select="'Untitled'"/>
<xsl:param name="author-filter"/>

Using goxslt from Go

The library API is small. The minimum is compile, transform, serialize:

package main

import (
    "fmt"
    "os"

    "github.com/speedata/goxml"
    "github.com/speedata/goxslt"
)

func main() {
    sourceFile, _ := os.Open("books.xml")
    sourceDoc, _ := goxml.Parse(sourceFile)
    sourceFile.Close()

    ss, err := goxslt.CompileFile("books.xsl")
    if err != nil {
        panic(err)
    }

    result, err := goxslt.Transform(ss, sourceDoc)
    if err != nil {
        panic(err)
    }

    fmt.Println(goxslt.SerializeResult(result.Document))
}

For passing parameters, output options, or running an xsl:initial-template, use TransformWithOptions and pass a goxslt.TransformOptions struct.

Where to next