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 ( ErrXSLTFailure = errors.New("xsl transformation failed") ErrXSLParseFailure = errors.New("failed to parse xsl") )
Package errors.
Functions ¶
This section is empty.
Types ¶
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) ([]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.
Click to show internal directories.
Click to hide internal directories.