-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample_test.go
87 lines (80 loc) · 1.99 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package xslt_test
import (
"fmt"
"github.com/wamuir/go-xslt"
)
func 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>
}