/* gonuts10seqB.go - https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/tf4aDQ1Hn_c Objective: to quote from email ================================ BEGIN QUOTE I'm actually dealing with Microsoft webtest files. An example can be find at, https://gist.github.com/suntong/e4dcdc6c85dcf769eec4 It is the same as our case -- we have comments before each " Under it, the requests and comments are grouped into this transaction. Now the challenge is, for *each* request with a comment immediately before it, change it attribute "ReportingName="""''s value with the content from its leading comments, and from the content of the grouping transaction as well. Let's say, first 10 chars or first three words or each. So for the first Request under "", which is " no Items.Comment elements continue } // Don't assume that Comment is an array. // There may be just one value, in which case it will decode as map[string]interface{}. switch c.(type) { case map[string]interface{}: c = []interface{}{c} } cmt := c.([]interface{}) // get the Request list r, ok := vm["Request"] if !ok { // --> no Items.Request elements continue } // Don't assume the Request is an array. // There may be just one value, in which case it will decode as map[string]interface{}. switch r.(type) { case map[string]interface{}: r = []interface{}{r} } req := r.([]interface{}) // fmt.Println("Comment:", cmt) // fmt.Println("Request:", req) // Comment elements with #seq==n are followed by Request element with #seq==n+1. // For each Comment.#seq==n extract the CommentText attribute value and use it to // set the ReportingName attribute value in Request.#seq==n+1. for _, v := range cmt { vmap := v.(map[string]interface{}) seq := vmap["#seq"].(int) // type is int // extract CommentText attr from array of "#attr" acmt, _ := mxj.Map(vmap).ValueForPathString("#attr.CommentText.#text") if acmt == "" { fmt.Println("no CommentText value in Comment attributes") } // fmt.Println(seq, acmt) // find the request with the #seq==seq+1 value var r map[string]interface{} for _, vv := range req { rt := vv.(map[string]interface{}) if rt["#seq"].(int) == seq+1 { r = rt break } } if r == nil { // no Request with #seq==seq+1 continue } if err := mxj.Map(r).SetValueForPath(tname+", "+acmt, "#attr.ReportingName.#text"); err != nil { fmt.Println("SetValueForPath err:", err) break } } } // re-encode the map with the TransactionTimer.#attr.Name & Items.Comment[#seq==n].#attr.CommentText // values copied to the Items.Request[#seq==n+1].#attr.ReportingName elements. b, err := m.XmlSeqIndent("", " ") if err != nil { fmt.Println("XmlIndent err:", err) return } fmt.Println(string(b)) } } var data = []byte(` `)