Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locate and modify #29

Closed
suntong opened this issue Nov 3, 2016 · 2 comments
Closed

Locate and modify #29

suntong opened this issue Nov 3, 2016 · 2 comments

Comments

@suntong
Copy link

suntong commented Nov 3, 2016

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.

@clbanning
Copy link
Owner

clbanning commented Nov 3, 2016

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.

There is a go JPath implementation here:

@clbanning clbanning reopened this Nov 4, 2016
@suntong
Copy link
Author

suntong commented Nov 4, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants