xslt

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 5 Imported by: 6

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 xml 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 Stylesheet xs from xsl stylesheet style.
xs, err := xslt.NewStylesheet(style)
if err != nil {
	panic(err)
}
defer xs.Close()

// Transform xml document doc using Stylesheet xs.
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 (
	ErrMixedQuotes     = errors.New("unable to quote parameter value")
	ErrUTF8Validation  = errors.New("input failed utf-8 validation")
	ErrXSLTFailure     = errors.New("xsl transformation failed")
	ErrXSLParseFailure = errors.New("failed to parse xsl")
)

Package errors.

Functions

This section is empty.

Types

type Parameter added in v0.1.4

type Parameter interface {
	// contains filtered or unexported methods
}

Parameter is a parameter to be passed to a stylesheet.

type StringParameter added in v0.1.4

type StringParameter struct {
	Name  string
	Value string
}

StringParameter is a stylesheet parameter consisting of a name/value pair, where name and value are UTF-8 strings.

type Stylesheet

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

Stylesheet represents an xsl stylesheet.

func NewStylesheet

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

NewStylesheet creates and returns new stylesheet xs along with any error. The resulting stylesheet may 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, params ...Parameter) ([]byte, error)

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

type XPathParameter added in v0.1.4

type XPathParameter struct {
	Name  string
	Value string
}

XPathParameter is a stylesheet parameter consisting of a name/value pair, where name is a QName or a UTF-8 string of the form {URI}NCName and value is a UTF-8 XPath expression. A quoted value (single or double) will be treated as a string rather than as an XPath expression, however the use of StringParameter is preferable when passing string parameters to a stylesheet.

Jump to

Keyboard shortcuts

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