Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
Initial gxpath project.
  • Loading branch information
zhengchun committed Oct 9, 2016
0 parents commit e9750f8
Show file tree
Hide file tree
Showing 17 changed files with 2,794 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
@@ -0,0 +1,32 @@
# vscode
.vscode
debug
*.test

./build

# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so


# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
17 changes: 17 additions & 0 deletions LICENSE
@@ -0,0 +1,17 @@
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
23 changes: 23 additions & 0 deletions README.md
@@ -0,0 +1,23 @@
gxpath
====
gxpath is a XPath 2.0 Go packages, that lets you extract data from the custom documents using XPath expression.

**[XQuery](https://github.com/antchfx/xquery)** : gxpath implementation, lets you extract data from HTML/XML documents using XPath.

Features
====
1. Standard path expression and axis name path.

2. Numeric functions and logical operator.. (+,-,div,mod,=,>,<,>=,<=,!=)

3. Boolean operator. (or,and)

4. Supported build-in functions.
- position()
- last()
- count()
- coming more

Examples
====
[main.go](http://github.com/antchfx/gxpath/example) : you can test XPath expr supported.
22 changes: 22 additions & 0 deletions example/README.md
@@ -0,0 +1,22 @@
test xml document
====

```xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en" id="1">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="en" id="2">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
```

How to run test example
===
1. go run main.go

2. enter your XPath expression.(etc,//book,//@lang,//book[price>20])
207 changes: 207 additions & 0 deletions example/main.go
@@ -0,0 +1,207 @@
package main

import (
"bufio"
"bytes"
"fmt"
"os"
"strings"

"github.com/antchfx/gxpath"
"github.com/antchfx/gxpath/xpath"
"github.com/antchfx/xml"
)

type xmlNodeNavigator struct {
doc *xml.Node
currnode *xml.Node
attindex int
}

func (n *xmlNodeNavigator) LocalName() string {
if n.attindex != -1 && len(n.currnode.Attr) > 0 {
return n.currnode.Attr[n.attindex].Name.Local
} else {
return n.currnode.Data
}
}

func (n *xmlNodeNavigator) Current() xpath.Node {
return n.currnode
}

func (n *xmlNodeNavigator) Value() string {
switch n.currnode.Type {
case xml.CommentNode:
return n.currnode.Data
case xml.DocumentNode:
return ""
case xml.ElementNode:
if n.attindex != -1 {
return n.currnode.Attr[n.attindex].Value
}
return XmlNodeInnerText(n.currnode)
case xml.TextNode:
return n.currnode.Data
default:
panic(fmt.Sprintf("unknowed XmlNodeType: %v", n.currnode.Type))
}
}

func (n *xmlNodeNavigator) Prefix() string {
return ""
}

func (n *xmlNodeNavigator) NodeType() xpath.NodeType {
switch n.currnode.Type {
case xml.CommentNode:
return xpath.CommentNode
case xml.TextNode:
return xpath.TextNode
case xml.DeclarationNode, xml.DocumentNode:
return xpath.RootNode
case xml.ElementNode:
if n.attindex != -1 {
return xpath.AttributeNode
}
return xpath.ElementNode
default:
panic(fmt.Sprintf("unknowed XmlNodeType: %v", n.currnode.Type))
}
}

func (n *xmlNodeNavigator) Copy() xpath.NodeNavigator {
nav := *n
return &nav
}

func (n *xmlNodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
nav, ok := other.(*xmlNodeNavigator)
if !ok {
return false
}
if nav.doc == n.doc {
n.currnode = nav.currnode
n.attindex = nav.attindex
return true
}
return false
}

func (n *xmlNodeNavigator) MoveToRoot() {
n.currnode = n.doc
}

func (n *xmlNodeNavigator) MoveToPrevious() bool {
if n.currnode.PrevSibling == nil {
return false
}
n.currnode = n.currnode.PrevSibling
return true
}

func (n *xmlNodeNavigator) MoveToParent() bool {
if n.currnode.Parent == nil {
return false
}
n.currnode = n.currnode.Parent
return true
}

func (n *xmlNodeNavigator) MoveToFirst() bool {
if n.currnode.PrevSibling == nil {
return false
}
for n.currnode.PrevSibling != nil {
n.currnode = n.currnode.PrevSibling
}
return true
}

func (n *xmlNodeNavigator) String() string {
return n.Value()
}

func (n *xmlNodeNavigator) MoveToNext() bool {
if cur := n.currnode.NextSibling; cur == nil {
return false
} else {
n.currnode = cur
}
return true
}

func (n *xmlNodeNavigator) MoveToFirstAttribute() bool {
if len(n.currnode.Attr) == 0 {
return false
}
n.attindex = 0
return true
}

func (n *xmlNodeNavigator) MoveToNextAttribute() bool {
if n.attindex >= len(n.currnode.Attr)-1 {
return false
}
n.attindex++
return true
}

func (n *xmlNodeNavigator) MoveToChild() bool {
if cur := n.currnode.FirstChild; cur == nil {
return false
} else {
n.currnode = cur
}
return true
}

func XmlNodeInnerText(n *xml.Node) string {
var b bytes.Buffer
var output func(*xml.Node)
output = func(node *xml.Node) {
if node.Type == xml.TextNode {
b.WriteString(node.Data)
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
output(child)
}
}
output(n)
return b.String()
}

func main() {
s := `<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="en" id="1">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="en" id="2">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
`
root, err := xml.Parse(strings.NewReader(s))
if err != nil {
panic(err)
}
nav := &xmlNodeNavigator{doc: root, currnode: root, attindex: -1}
fmt.Println("enter XPath express and press enter key to test.")
scan := bufio.NewScanner(os.Stdin)
for {
scan.Scan()
iter, err := gxpath.Select(nav, strings.TrimSpace(scan.Text()))
if err != nil {
fmt.Println(fmt.Sprintf("got error: %v", err))
} else {
for iter.MoveNext() {
fmt.Println(">>")
fmt.Println(iter.Current().Value())
}
}
fmt.Println("==========")
}
}

0 comments on commit e9750f8

Please sign in to comment.