Example #1
0
func findNamespace(x tree.Node, p *pathexpr.PathExpr, ret *[]tree.Node) {
	if ele, ok := x.(tree.NSElem); ok {
		ns := tree.BuildNS(ele)
		for _, i := range ns {
			addNode(i, p, ret)
		}
	}
}
Example #2
0
func setEle(opts *xmlbuilder.BuilderOpts, xmlTree xmlbuilder.XMLBuilder, ele xml.StartElement, ordrPos *int) {
	opts.NodePos = *ordrPos
	opts.Tok = ele
	opts.Attrs = opts.Attrs[0:0:cap(opts.Attrs)]
	opts.NS = make(map[xml.Name]string)
	opts.NodeType = tree.NtElem

	for i := range ele.Attr {
		attr := ele.Attr[i].Name
		val := ele.Attr[i].Value

		if (attr.Local == "xmlns" && attr.Space == "") || attr.Space == "xmlns" {
			opts.NS[attr] = val
		} else {
			opts.Attrs = append(opts.Attrs, &ele.Attr[i])
		}
	}

	if nstree, ok := xmlTree.(tree.NSElem); ok {
		ns := make(map[xml.Name]string)

		for _, i := range tree.BuildNS(nstree) {
			ns[i.Name] = i.Value
		}

		for k, v := range opts.NS {
			ns[k] = v
		}

		if ns[xml.Name{Local: "xmlns"}] == "" {
			delete(ns, xml.Name{Local: "xmlns"})
		}

		for k, v := range ns {
			opts.NS[k] = v
		}

		if xmlTree.GetNodeType() == tree.NtRoot {
			opts.NS[xml.Name{Space: "xmlns", Local: "xml"}] = tree.XMLSpace
		}
	}

	opts.AttrStartPos = len(opts.NS) + len(opts.Attrs) + *ordrPos
	*ordrPos = opts.AttrStartPos + 1
}