• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H03-May-2022-233218

html/H15-May-2018-399340

xml/H15-May-2018-900785

.gitignoreH A D15-May-2018306 3224

.travis.ymlH A D15-May-2018258 1512

LICENSEH A D15-May-20181,022 1715

README.mdH A D15-May-20183 KiB10783

README.md

1xquery
2====
3[![Build Status](https://travis-ci.org/antchfx/xquery.svg?branch=master)](https://travis-ci.org/antchfx/xquery)
4[![Coverage Status](https://coveralls.io/repos/github/antchfx/xquery/badge.svg?branch=master)](https://coveralls.io/github/antchfx/xquery?branch=master)
5[![GoDoc](https://godoc.org/github.com/antchfx/xquery?status.svg)](https://godoc.org/github.com/antchfx/xquery)
6[![Go Report Card](https://goreportcard.com/badge/github.com/antchfx/xquery)](https://goreportcard.com/report/github.com/antchfx/xquery)
7
8> NOTE: This package is deprecated. Recommends use [htmlquery](https://github.com/antchfx/htmlquery) and [xmlquery](https://github.com/antchfx/xmlquery) package, get latest version to fixed some issues.
9
10Overview
11===
12
13Golang package, lets you extract data from HTML/XML documents using XPath expression.
14
15List of supported XPath functions you can found here [XPath Package](https://github.com/antchfx/xpath).
16
17Installation
18====
19
20> go get github.com/antchfx/xquery
21
22
23HTML Query [![GoDoc](https://godoc.org/github.com/antchfx/xquery/html?status.svg)](https://godoc.org/github.com/antchfx/xquery/html)
24===
25
26Extract data from HTML document.
27
28```go
29package main
30
31import (
32	"github.com/antchfx/xpath"
33	"github.com/antchfx/xquery/html"
34)
35
36func main() {
37	// Load HTML file.
38	f, err := os.Open(`./examples/test.html`)
39	if err != nil {
40		panic(err)
41	}
42	// Parse HTML document.
43	doc, err := htmlquery.Parse(f)
44	if err != nil{
45		panic(err)
46	}
47
48	// Option 1: using xpath's expr to matches nodes.
49	expr := xpath.MustCompile("count(//div[@class='article'])")
50	fmt.Printf("%f \n", expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(float64))
51
52	expr = xpath.MustCompile("//a/@href")
53	iter := expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(*xpath.NodeIterator)
54	for iter.MoveNext() {
55		fmt.Printf("%s \n", iter.Current().Value()) // output href
56	}
57
58	// Option 2: using build-in functions Find() to matches nodes.
59	for _, n := range htmlquery.Find(doc, "//a/@href") {
60		fmt.Printf("%s \n", htmlquery.SelectAttr(n, "href")) // output href
61	}
62}
63```
64
65XML Query [![GoDoc](https://godoc.org/github.com/antchfx/xquery/xml?status.svg)](https://godoc.org/github.com/antchfx/xquery/xml)
66===
67Extract data from XML document.
68
69```go
70package main
71
72import (
73	"github.com/antchfx/xpath"
74	"github.com/antchfx/xquery/xml"
75)
76
77func main() {
78	// Load XML document from file.
79	f, err := os.Open(`./examples/test.xml`)
80	if err != nil {
81		panic(err)
82	}
83	// Parse XML document.
84	doc, err := xmlquery.Parse(f)
85	if err != nil{
86		panic(err)
87	}
88
89	// Option 1: using xpath's expr to matches nodes.
90
91	// sum all book's price via Evaluate()
92	expr, err := xpath.Compile("sum(//book/price)")
93	if err != nil {
94		panic(err)
95	}
96	fmt.Printf("total price: %f\n", expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64))
97
98	for _, n := range xmlquery.Find(doc, "//book") {
99		fmt.Printf("%s : %s \n", n.SelectAttr("id"), xmlquery.FindOne(n, "title").InnerText())
100	}
101
102	// Option 2: using build-in functions FindOne() to matches node.
103	n := xmlquery.FindOne(doc, "//book[@id='bk104']")
104	fmt.Printf("%s \n", n.OutputXML(true))
105}
106```
107