xslt

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2021 License: MIT Imports: 3 Imported by: 7

README

go-xslt

Go Reference Build Status codecov Go Report Card

Description

go-xslt is a Go module that performs basic XSLT 1.0 transformations via Libxslt.

Installation

You'll need the development libraries for libxml2 and libxslt, along with those for liblzma and zlib. Install these via your package manager. For instance, if using apt then:

sudo apt install libxml2-dev libxslt1-dev liblzma-dev zlib1g-dev

This module can be installed with the go get command:

go get -u github.com/wamuir/go-xslt

Usage


  // style is an XSLT 1.0 stylesheet, as []byte.
  xs, err := xslt.NewStylesheet(style)
  if err != nil {
      panic(err)
  }
  defer xs.Close()

  // doc is an XML document to be transformed and res is the result of
  // the XSL transformation, both as []byte. 
  res, err := xs.Transform(doc)
  if err != nil {
      panic(err)
  }

Documentation

Overview

Example
// doc is the document to be transformed
var doc = []byte(
	`<?xml version="1.0" ?>
		 <persons>
		   <person username="JS1">
		     <name>John</name>
		     <family-name>Smith</family-name>
		   </person>
		   <person username="MI1">
		     <name>Morka</name>
		     <family-name>Ismincius</family-name>
		   </person>
		 </persons>`,
)

// style is the XSL stylesheet to be used for transformation
var style = []byte(
	`<?xml version="1.0" encoding="UTF-8"?>
		 <xsl:stylesheet
		   version="1.0"
		   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		   xmlns="http://www.w3.org/1999/xhtml">
		   <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
		   <xsl:template match="/persons">
		     <html>
		       <head>
			 <title>Testing XML Example</title>
		       </head>
		       <body>
			 <h1>Persons</h1>
			 <ul>
			   <xsl:apply-templates select="person">
			     <xsl:sort select="family-name" />
			   </xsl:apply-templates>
			 </ul>
		       </body>
		     </html>
		   </xsl:template>
		   <xsl:template match="person">
		     <li>
		       <xsl:value-of select="family-name"/><xsl:text>, </xsl:text><xsl:value-of select="name"/>
		     </li>
		   </xsl:template>
		 </xsl:stylesheet>`,
)

// create a new stylesheet from style
xs, err := xslt.NewStylesheet(style)
if err != nil {
	panic(err)
}
defer xs.Close()

// transform the document using the style
res, err := xs.Transform(doc)
if err != nil {
	panic(err)
}

// print the result of the transformation
fmt.Println(string(res))
Output:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Testing XML Example</title>
  </head>
  <body>
    <h1>Persons</h1>
    <ul>
      <li>Ismincius, Morka</li>
      <li>Smith, John</li>
    </ul>
  </body>
</html>

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrXSLTFailure     = errors.New("XSL transformation failed")
	ErrXSLParseFailure = errors.New("Failed to parse XSL")
)

Transformation errors

Functions

This section is empty.

Types

type Stylesheet

type Stylesheet struct {
	// contains filtered or unexported fields
}

Stylesheet represents an XSL

func NewStylesheet

func NewStylesheet(xsl []byte) (*Stylesheet, error)

NewStylesheet creates and returns a new stylesheet along with any errors. The resulting stylesheet ay be nil if an error is encountered during parsing. This implementation relies on Libxslt, which supports XSLT 1.0.

func (*Stylesheet) Close

func (xs *Stylesheet) Close()

Close frees memory associated with a stylesheet. Additional calls to Close will be ignored.

func (*Stylesheet) Transform

func (xs *Stylesheet) Transform(xml []byte) ([]byte, error)

Transform applies the receiver to the XML and returns the result of an XSL transformation and any errors. The resulting document may be nil (a zero-length and zero-capacity byte slice) in the case of an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL