-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
demo test:libxslt dep libxml
1 parent
f2955ca
commit eac91f3
Showing
2 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"unsafe" | ||
|
||
"libxslt" | ||
|
||
"github.com/goplus/llgo/c" | ||
libxml2 "github.com/luoliwoshang/llcppg-libxml" | ||
) | ||
|
||
func main() { | ||
libxml2.XmlInitParser() | ||
|
||
xml := | ||
`<?xml version='1.0'?> | ||
<root> | ||
<person> | ||
<name>Alice</name> | ||
<age>25</age> | ||
</person> | ||
</root>` | ||
xslt := `<?xml version="1.0" encoding="UTF-8"?> | ||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | ||
<xsl:template match="/"> | ||
<html> | ||
<body> | ||
<h1>个人信息</h1> | ||
<p>姓名: <xsl:value-of select="root/person/name"/></p> | ||
<p>年龄: <xsl:value-of select="root/person/age"/></p> | ||
</body> | ||
</html> | ||
</xsl:template> | ||
</xsl:stylesheet> | ||
` | ||
xmlDoc := libxml2.XmlReadMemory((*int8)(unsafe.Pointer(unsafe.StringData(xml))), c.Int(len(xml)), nil, nil, 0) | ||
xsltDoc := libxml2.XmlReadMemory((*int8)(unsafe.Pointer(unsafe.StringData(xslt))), c.Int(len(xslt)), nil, nil, 0) | ||
|
||
if xmlDoc == nil || xsltDoc == nil { | ||
panic("cant read xml or xslt") | ||
} | ||
|
||
stylesheet := libxslt.XsltParseStylesheetDoc(xsltDoc) | ||
if stylesheet == nil { | ||
panic("cant parse xslt") | ||
} | ||
result := libxslt.XsltApplyStylesheet(stylesheet, xmlDoc, (**int8)(unsafe.Pointer(uintptr(0)))) | ||
if result == nil { | ||
panic("cant apply xslt") | ||
} | ||
|
||
libxslt.XsltSaveResultToFilename(c.Str("output.html"), result, stylesheet, 0) | ||
libxslt.XsltFreeStylesheet(stylesheet) | ||
|
||
libxml2.XmlFreeDoc(xmlDoc) | ||
libxml2.XmlFreeDoc(xsltDoc) | ||
libxml2.XmlFreeDoc(result) | ||
|
||
libxslt.XsltCleanupGlobals() | ||
libxml2.XmlCleanupParser() | ||
|
||
buf, err := os.ReadFile("./output.html") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(string(buf)) | ||
} |