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 ¶
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
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
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.