Skip to content

Commit eac191f

Browse files
committed
Improve documentation
1 parent def09ea commit eac191f

File tree

4 files changed

+65
-49
lines changed

4 files changed

+65
-49
lines changed

README.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ xmlquery
88
Overview
99
===
1010

11-
`xmlquery` is an XPath query package for XML document, lets you extract data or evaluate from XML documents by an XPath expression.
11+
`xmlquery` is an XPath query package for XML documents, allowing you to extract
12+
data or evaluate from XML documents with an XPath expression.
1213

13-
`xmlquery` built-in the query object caching feature will caching the recently used XPATH query string. Enable caching can avoid re-compile XPath expression each query.
14+
`xmlquery` has a built-in query object caching feature that caches recently used
15+
XPATH query strings. Enabling caching can avoid recompile XPath expression for
16+
each query.
1417

1518
Change Logs
1619
===
@@ -22,16 +25,16 @@ Change Logs
2225
- Add XPath query caching.
2326

2427
2019-10-05
25-
- Add new methods that compatible with invalid XPath expression error: `QueryAll` and `Query`.
26-
- Add `QuerySelector` and `QuerySelectorAll` methods, supported reused your query object.
28+
- Add new methods compatible with invalid XPath expression error: `QueryAll` and `Query`.
29+
- Add `QuerySelector` and `QuerySelectorAll` methods, support for reused query objects.
2730
- PR [#12](https://github.com/antchfx/xmlquery/pull/12) (Thanks @FrancescoIlario)
2831
- PR [#11](https://github.com/antchfx/xmlquery/pull/11) (Thanks @gjvnq)
2932

3033
2018-12-23
31-
- added XML output will including comment node. [#9](https://github.com/antchfx/xmlquery/issues/9)
34+
- Added XML output including comment nodes. [#9](https://github.com/antchfx/xmlquery/issues/9)
3235

3336
2018-12-03
34-
- added support attribute name with namespace prefix and XML output. [#6](https://github.com/antchfx/xmlquery/issues/6)
37+
- Added support to attribute name with namespace prefix and XML output. [#6](https://github.com/antchfx/xmlquery/issues/6)
3538

3639
Installation
3740
====
@@ -71,7 +74,7 @@ f, err := os.Open("../books.xml")
7174
doc, err := xmlquery.Parse(f)
7275
```
7376

74-
#### Parse an XML in a stream fashion (simple case without element filtering).
77+
#### Parse an XML in a stream fashion (simple case without elements filtering).
7578

7679
```go
7780
f, err := os.Open("../books.xml")
@@ -117,33 +120,33 @@ list := xmlquery.Find(doc, "//author")
117120
book := xmlquery.FindOne(doc, "//book[2]")
118121
```
119122

120-
#### Find all book elements and only get `id` attribute self. (New Feature)
123+
#### Find all book elements and only get `id` attribute. (New Feature)
121124

122125
```go
123126
list := xmlquery.Find(doc,"//book/@id")
124127
```
125128

126-
#### Find all books with id is bk104.
129+
#### Find all books with id `bk104`.
127130

128131
```go
129132
list := xmlquery.Find(doc, "//book[@id='bk104']")
130133
```
131134

132-
#### Find all books that price less than 5.
135+
#### Find all books with price less than 5.
133136

134137
```go
135138
list := xmlquery.Find(doc, "//book[price<5]")
136139
```
137140

138-
#### Evaluate the total price of all books.
141+
#### Evaluate total price of all books.
139142

140143
```go
141144
expr, err := xpath.Compile("sum(//book/price)")
142145
price := expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64)
143146
fmt.Printf("total price: %f\n", price)
144147
```
145148

146-
#### Evaluate the number of all books element.
149+
#### Evaluate number of all book elements.
147150

148151
```go
149152
expr, err := xpath.Compile("count(//book)")
@@ -155,14 +158,17 @@ FAQ
155158

156159
#### `Find()` vs `QueryAll()`, which is better?
157160

158-
`Find` and `QueryAll` both do the same things, searches all of matched html nodes.
159-
The `Find` will panics if you give an error XPath query, but `QueryAll` will return an error for you.
161+
`Find` and `QueryAll` both do the same thing: searches all of matched XML nodes.
162+
`Find` panics if provided with an invalid XPath query, while `QueryAll` returns
163+
an error.
160164

161165
#### Can I save my query expression object for the next query?
162166

163-
Yes, you can. We offer the `QuerySelector` and `QuerySelectorAll` methods, It will accept your query expression object.
167+
Yes, you can. We provide `QuerySelector` and `QuerySelectorAll` methods; they
168+
accept your query expression object.
164169

165-
Cache a query expression object(or reused) will avoid re-compile XPath query expression, improve your query performance.
170+
Caching a query expression object avoids recompiling the XPath query
171+
expression, improving query performance.
166172

167173
#### Create XML document.
168174

@@ -247,9 +253,9 @@ List of supported XPath query packages
247253
===
248254
| Name | Description |
249255
| ------------------------------------------------- | ----------------------------------------- |
250-
| [htmlquery](https://github.com/antchfx/htmlquery) | XPath query package for the HTML document |
251-
| [xmlquery](https://github.com/antchfx/xmlquery) | XPath query package for the XML document |
252-
| [jsonquery](https://github.com/antchfx/jsonquery) | XPath query package for the JSON document |
256+
| [htmlquery](https://github.com/antchfx/htmlquery) | XPath query package for HTML documents |
257+
| [xmlquery](https://github.com/antchfx/xmlquery) | XPath query package for XML documents |
258+
| [jsonquery](https://github.com/antchfx/jsonquery) | XPath query package for JSON documents |
253259

254260
Questions
255261
===

node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const (
1414
// DocumentNode is a document object that, as the root of the document tree,
1515
// provides access to the entire XML document.
1616
DocumentNode NodeType = iota
17-
// DeclarationNode is the document type declaration, indicated by the following
18-
// tag (for example, <!DOCTYPE...> ).
17+
// DeclarationNode is the document type declaration, indicated by the
18+
// following tag (for example, <!DOCTYPE...> ).
1919
DeclarationNode
2020
// ElementNode is an element (for example, <item> ).
2121
ElementNode

parse.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (p *parser) parse() (*Node, error) {
117117
Attr: tok.Attr,
118118
level: p.level,
119119
}
120-
//fmt.Println(fmt.Sprintf("start > %s : %d", node.Data, node.level))
120+
121121
if p.level == p.prev.level {
122122
AddSibling(p.prev, node)
123123
} else if p.level > p.prev.level {
@@ -227,13 +227,16 @@ func (p *parser) parse() (*Node, error) {
227227
}
228228
}
229229

230-
// StreamParser enables loading and parsing an XML document in a streaming fashion.
230+
// StreamParser enables loading and parsing an XML document in a streaming
231+
// fashion.
231232
type StreamParser struct {
232233
p *parser
233234
}
234235

235-
// CreateStreamParser creates a StreamParser. Argument streamElementXPath is required.
236-
// Argument streamElementFilter is optional and should only be used in advanced scenarios.
236+
// CreateStreamParser creates a StreamParser. Argument streamElementXPath is
237+
// required.
238+
// Argument streamElementFilter is optional and should only be used in advanced
239+
// scenarios.
237240
//
238241
// Scenario 1: simple case:
239242
// xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>`
@@ -268,12 +271,15 @@ type StreamParser struct {
268271
// Output will be:
269272
// <BBB>b2</BBB>
270273
//
271-
// As the argument names indicate, streamElementXPath should be used for providing xpath query pointing
272-
// to the target element node only, no extra filtering on the element itself or its children; while
273-
// streamElementFilter, if needed, can provide additional filtering on the target element and its children.
274+
// As the argument names indicate, streamElementXPath should be used for
275+
// providing xpath query pointing to the target element node only, no extra
276+
// filtering on the element itself or its children; while streamElementFilter,
277+
// if needed, can provide additional filtering on the target element and its
278+
// children.
274279
//
275-
// CreateStreamParser returns error if either streamElementXPath or streamElementFilter, if provided, cannot
276-
// be successfully parsed and compiled into a valid xpath query.
280+
// CreateStreamParser returns an error if either streamElementXPath or
281+
// streamElementFilter, if provided, cannot be successfully parsed and compiled
282+
// into a valid xpath query.
277283
func CreateStreamParser(r io.Reader, streamElementXPath string, streamElementFilter ...string) (*StreamParser, error) {
278284
elemXPath, err := getQuery(streamElementXPath)
279285
if err != nil {
@@ -294,12 +300,13 @@ func CreateStreamParser(r io.Reader, streamElementXPath string, streamElementFil
294300
return sp, nil
295301
}
296302

297-
// Read returns a target node that satisifies the XPath specified by caller at StreamParser creation
298-
// time. If there is no more satisifying target node after reading the rest of the XML document, io.EOF
299-
// will be returned. At any time, any XML parsing error encountered, the error will be returned and
300-
// the stream parsing is stopped. Calling Read() after an error is returned (including io.EOF) is not
301-
// allowed the behavior will be undefined. Also note, due to the streaming nature, calling Read() will
302-
// automatically remove any previous target node(s) from the document tree.
303+
// Read returns a target node that satisfies the XPath specified by caller at
304+
// StreamParser creation time. If there is no more satisfying target nodes after
305+
// reading the rest of the XML document, io.EOF will be returned. At any time,
306+
// any XML parsing error encountered will be returned, and the stream parsing
307+
// stopped. Calling Read() after an error is returned (including io.EOF) results
308+
// undefined behavior. Also note, due to the streaming nature, calling Read()
309+
// will automatically remove any previous target node(s) from the document tree.
303310
func (sp *StreamParser) Read() (*Node, error) {
304311
// Because this is a streaming read, we need to release/remove last
305312
// target node from the node tree to free up memory.

query.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ func (n *Node) SelectAttr(name string) string {
4444

4545
var _ xpath.NodeNavigator = &NodeNavigator{}
4646

47-
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.
47+
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified
48+
// XML Node.
4849
func CreateXPathNavigator(top *Node) *NodeNavigator {
4950
return &NodeNavigator{curr: top, root: top, attr: -1}
5051
}
@@ -67,8 +68,8 @@ func getCurrentNode(it *xpath.NodeIterator) *Node {
6768
return n.curr
6869
}
6970

70-
// Find is like QueryAll but it will panics if the `expr` is not a
71-
// valid XPath expression. See `QueryAll()` function.
71+
// Find is like QueryAll but panics if `expr` is not a valid XPath expression.
72+
// See `QueryAll()` function.
7273
func Find(top *Node, expr string) []*Node {
7374
nodes, err := QueryAll(top, expr)
7475
if err != nil {
@@ -77,8 +78,8 @@ func Find(top *Node, expr string) []*Node {
7778
return nodes
7879
}
7980

80-
// FindOne is like Query but it will panics if the `expr` is not a
81-
// valid XPath expression. See `Query()` function.
81+
// FindOne is like Query but panics if `expr` is not a valid XPath expression.
82+
// See `Query()` function.
8283
func FindOne(top *Node, expr string) *Node {
8384
node, err := Query(top, expr)
8485
if err != nil {
@@ -88,7 +89,7 @@ func FindOne(top *Node, expr string) *Node {
8889
}
8990

9091
// QueryAll searches the XML Node that matches by the specified XPath expr.
91-
// Return an error if the expression `expr` cannot be parsed.
92+
// Returns an error if the expression `expr` cannot be parsed.
9293
func QueryAll(top *Node, expr string) ([]*Node, error) {
9394
exp, err := getQuery(expr)
9495
if err != nil {
@@ -98,7 +99,7 @@ func QueryAll(top *Node, expr string) ([]*Node, error) {
9899
}
99100

100101
// Query searches the XML Node that matches by the specified XPath expr,
101-
// and returns first element of matched.
102+
// and returns first matched element.
102103
func Query(top *Node, expr string) (*Node, error) {
103104
exp, err := getQuery(expr)
104105
if err != nil {
@@ -107,7 +108,8 @@ func Query(top *Node, expr string) (*Node, error) {
107108
return QuerySelector(top, exp), nil
108109
}
109110

110-
// QuerySelectorAll searches all of the XML Node that matches the specified XPath selectors.
111+
// QuerySelectorAll searches all of the XML Node that matches the specified
112+
// XPath selectors.
111113
func QuerySelectorAll(top *Node, selector *xpath.Expr) []*Node {
112114
t := selector.Select(CreateXPathNavigator(top))
113115
var elems []*Node
@@ -117,7 +119,8 @@ func QuerySelectorAll(top *Node, selector *xpath.Expr) []*Node {
117119
return elems
118120
}
119121

120-
// QuerySelector returns the first matched XML Node by the specified XPath selector.
122+
// QuerySelector returns the first matched XML Node by the specified XPath
123+
// selector.
121124
func QuerySelector(top *Node, selector *xpath.Expr) *Node {
122125
t := selector.Select(CreateXPathNavigator(top))
123126
if t.MoveNext() {
@@ -127,16 +130,16 @@ func QuerySelector(top *Node, selector *xpath.Expr) *Node {
127130
}
128131

129132
// FindEach searches the html.Node and calls functions cb.
130-
// Important: this method has deprecated, recommend use for .. = range Find(){}.
133+
// Important: this method is deprecated, instead, use for .. = range Find(){}.
131134
func FindEach(top *Node, expr string, cb func(int, *Node)) {
132135
for i, n := range Find(top, expr) {
133136
cb(i, n)
134137
}
135138
}
136139

137-
// FindEachWithBreak functions the same as FindEach but allows you
138-
// to break the loop by returning false from your callback function, cb.
139-
// Important: this method has deprecated, recommend use for .. = range Find(){}.
140+
// FindEachWithBreak functions the same as FindEach but allows to break the loop
141+
// by returning false from the callback function `cb`.
142+
// Important: this method is deprecated, instead, use .. = range Find(){}.
140143
func FindEachWithBreak(top *Node, expr string, cb func(int, *Node) bool) {
141144
for i, n := range Find(top, expr) {
142145
if !cb(i, n) {

0 commit comments

Comments
 (0)