Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demo test:libxslt dep libxml
Browse files Browse the repository at this point in the history
luoliwoshang committed Jan 14, 2025
1 parent f2955ca commit eac91f3
Showing 2 changed files with 72 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -92,11 +92,11 @@ jobs:
if: startsWith(matrix.os, 'macos')
run: |
# install demo's lib
brew install lua zlib isl libgpg-error raylib z3 sqlite3 gmp
brew install lua zlib isl libgpg-error raylib z3 sqlite3 gmp libxslt
export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig"
export PKG_CONFIG_PATH="/opt/homebrew/opt/sqlite/lib/pkgconfig:/opt/homebrew/opt/zlib/lib/pkgconfig:$PKG_CONFIG_PATH"
export PKG_CONFIG_PATH="/opt/homebrew/opt/sqlite/lib/pkgconfig:$PKG_CONFIG_PATH"
export PKG_CONFIG_PATH="/opt/homebrew/opt/libxslt/lib/pkgconfig:$PKG_CONFIG_PATH"
pkg-config --cflags --libs sqlite3
llcppgtest -demos ./_llcppgtest
69 changes: 69 additions & 0 deletions _llcppgtest/libxslt/demo/withdeplibxml/demo.go
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))
}

0 comments on commit eac91f3

Please sign in to comment.