Locate and modify
Closed this issue · 2 comments
Related to issue #28, what I am looking forward to in mxj, is able to locate some records according to XPath/JPath or something similar, them modify the located record.
Please take a look at this .xml file for example, I need to, expressed in xpath term,
- locate
//Request
nodes; and for each node found, - change its attribute according to other attributes.
For example, these changes were to change
//Request
's ./[ReportingName]
use the basename
of ./[Url]
( and prefix with ../TransactionTimer[Name]
as well, but that's a bonus request if it is too difficult at first).
The final result is posted here.
How can I do that? Thx.
I have posted a new example - https://github.com/clbanning/mxj/blob/master/examples/jpath.go - that demonstrates using this package to perform the JPath use cases in the 2nd reference you provided in issue #28. There are a number of methods that allow changing values in the Map itself:
- m.NewMap()
- m.Remove()
- m.SetValueForPath()
- m.UpdateValuesForPath()
See some use cases in https://github.com/clbanning/mxj/tree/master/examples - gonuts1a, gonuts10.go, gonuts10seq.go, gonuts11seq.go, and gonuts12seq.go are pertinent. (The last 4 are examples I did based on a gonuts posting you made last year - https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/tf4aDQ1Hn_c. In fact, gonuts11seq.go and gonuts12seq.go are working with XML that is [similar to] that which you reference, above. I think you'll find gonuts12seq.go doing something close to what you describe.)
The mxj package originated to decode XML to map[string]interface{} values as is supported by encoding/json stdlib package for generic data extraction. It is not focussed on directly manipulating XML ... only later was encoding map[string]interface{} values to XML added. The following two libs implement XPath in go.
- https://godoc.org/github.com/moovweb/gokogiri/xpath
- https://godoc.org/github.com/ChrisTrenkamp/goxpath
There is a go JPath implementation here:
Thanks.
I'll give it a try. I found a more expressive solution, a very elegant one which documented here (78 lines in total, including all the document texts):
the following were to change
//Request's ./[ReportingName] use the basename of ./[Url](and also prefixing it with ../TransactionTimer[Name] as well).
Thanks to your goxpath recommendation.