Skip to content

Commit c9d411c

Browse files
committed
Add new example code
1 parent ce135a7 commit c9d411c

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

‎README.md

+61-15
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,49 @@ expr, err := xpath.Compile("count(//book)")
190190
price := expr.Evaluate(xmlquery.CreateXPathNavigator(doc)).(float64)
191191
```
192192

193-
FAQ
193+
Advanced Features
194194
====
195195

196-
#### `Find()` vs `QueryAll()`, which is better?
197-
198-
`Find` and `QueryAll` both do the same thing: searches all of matched XML nodes.
199-
`Find` panics if provided with an invalid XPath query, while `QueryAll` returns
200-
an error.
196+
### Parse `UTF-16` XML file with `ParseWithOptions()`.
201197

202-
#### Can I save my query expression object for the next query?
198+
```go
199+
f, _ := os.Open(`UTF-16.XML`)
200+
// Convert UTF-16 XML to UTF-8
201+
utf16ToUtf8Transformer := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
202+
utf8Reader := transform.NewReader(f, utf16ToUtf8Transformer)
203+
// Sets `CharsetReader`
204+
options := xmlquery.ParserOptions{
205+
Decoder: &xmlquery.DecoderOptions{
206+
CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
207+
return input, nil
208+
},
209+
},
210+
}
211+
doc, err := xmlquery.ParseWithOptions(utf8Reader, options)
212+
```
203213

204-
Yes, you can. We provide `QuerySelector` and `QuerySelectorAll` methods; they
205-
accept your query expression object.
214+
### Query with custom namespace prefix.
206215

207-
Caching a query expression object avoids recompiling the XPath query
208-
expression, improving query performance.
216+
```go
217+
s := `<?xml version="1.0" encoding="UTF-8"?>
218+
<pd:ProcessDefinition xmlns:pd="http://xmlns.xyz.com/process/2003" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
219+
<pd:activity name="Invoke Request-Response Service">
220+
<pd:type>RequestReplyActivity</pd:type>
221+
<pd:resourceType>OpClientReqActivity</pd:resourceType>
222+
<pd:x>300</pd:x>
223+
<pd:y>80</pd:y>
224+
</pd:activity>
225+
</pd:ProcessDefinition>`
226+
nsMap := map[string]string{
227+
"q": "http://xmlns.xyz.com/process/2003",
228+
"r": "http://www.w3.org/1999/XSL/Transform",
229+
"s": "http://www.w3.org/2001/XMLSchema",
230+
}
231+
expr, _ := xpath.CompileWithNS("//q:activity", nsMap)
232+
node := xmlquery.QuerySelector(doc, expr)
233+
```
209234

210-
#### Create XML document.
235+
#### Create XML document without call `xml.Marshal`.
211236

212237
```go
213238
doc := &xmlquery.Node{
@@ -237,13 +262,34 @@ title_text := &xmlquery.Node{
237262
}
238263
title.FirstChild = title_text
239264
channel.FirstChild = title
240-
fmt.Println(doc.OutputXML(true))
241-
// <?xml version="1.0"?><rss><channel><title>W3Schools Home Page</title></channel></rss>
242265

266+
fmt.Println(doc.OutputXML(true))
243267
fmt.Println(doc.OutputXMLWithOptions(WithOutputSelf()))
244-
// <?xml version="1.0"?><rss><channel><title>W3Schools Home Page</title></channel></rss>
245268
```
246269

270+
Output:
271+
272+
```xml
273+
<?xml version="1.0"?><rss><channel><title>W3Schools Home Page</title></channel></rss>
274+
```
275+
276+
FAQ
277+
====
278+
279+
#### `Find()` vs `QueryAll()`, which is better?
280+
281+
`Find` and `QueryAll` both do the same thing: searches all of matched XML nodes.
282+
`Find` panics if provided with an invalid XPath query, while `QueryAll` returns
283+
an error.
284+
285+
#### Can I save my query expression object for the next query?
286+
287+
Yes, you can. We provide `QuerySelector` and `QuerySelectorAll` methods; they
288+
accept your query expression object.
289+
290+
Caching a query expression object avoids recompiling the XPath query
291+
expression, improving query performance.
292+
247293
Questions
248294
===
249295
Please let me know if you have any questions

0 commit comments

Comments
 (0)