@@ -190,24 +190,49 @@ expr, err := xpath.Compile("count(//book)")
190
190
price := expr.Evaluate (xmlquery.CreateXPathNavigator (doc)).(float64 )
191
191
```
192
192
193
- FAQ
193
+ Advanced Features
194
194
====
195
195
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() ` .
201
197
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
+ ```
203
213
204
- Yes, you can. We provide ` QuerySelector ` and ` QuerySelectorAll ` methods; they
205
- accept your query expression object.
214
+ ### Query with custom namespace prefix.
206
215
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
+ ```
209
234
210
- #### Create XML document.
235
+ #### Create XML document without call ` xml.Marshal ` .
211
236
212
237
``` go
213
238
doc := &xmlquery.Node {
@@ -237,13 +262,34 @@ title_text := &xmlquery.Node{
237
262
}
238
263
title.FirstChild = title_text
239
264
channel.FirstChild = title
240
- fmt.Println (doc.OutputXML (true ))
241
- // <?xml version="1.0"?><rss><channel><title>W3Schools Home Page</title></channel></rss>
242
265
266
+ fmt.Println (doc.OutputXML (true ))
243
267
fmt.Println (doc.OutputXMLWithOptions (WithOutputSelf ()))
244
- // <?xml version="1.0"?><rss><channel><title>W3Schools Home Page</title></channel></rss>
245
268
```
246
269
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
+
247
293
Questions
248
294
===
249
295
Please let me know if you have any questions
0 commit comments