@@ -106,13 +106,19 @@ func (p *parser) parse() (*Node, error) {
106106 }
107107 // https://www.w3.org/TR/xml-names/#scoping-defaulting
108108 var defaultNamespaceURL string
109+ // [#102], If found the duplicate NamespaceURL, we should saving into the loca
110+ // and use it instead of p.space2prefix.
111+ local_space2prefix := make (map [string ]string )
109112 for _ , att := range tok .Attr {
110113 if att .Name .Local == "xmlns" {
111114 p .space2prefix [att .Value ] = "" // reset empty if exist the default namespace
115+ local_space2prefix [att .Value ] = ""
112116 defaultNamespaceURL = att .Value
113117 } else if att .Name .Space == "xmlns" {
114118 if _ , ok := p .space2prefix [att .Value ]; ! ok {
115119 p .space2prefix [att .Value ] = att .Name .Local
120+ } else if defaultNamespaceURL != att .Value {
121+ local_space2prefix [att .Value ] = att .Name .Local
116122 }
117123 }
118124 }
@@ -155,10 +161,16 @@ func (p *parser) parse() (*Node, error) {
155161 AddSibling (p .prev .Parent , node )
156162 }
157163 if node .NamespaceURI != "" {
158- node .Prefix = p .space2prefix [node .NamespaceURI ]
164+ var keepPrefix bool = true
165+ if prefix , ok := local_space2prefix [node .NamespaceURI ]; ok {
166+ keepPrefix = true
167+ node .Prefix = prefix
168+ } else {
169+ node .Prefix = p .space2prefix [node .NamespaceURI ]
170+ }
159171 if defaultNamespaceURL != "" && node .NamespaceURI == defaultNamespaceURL {
160172 node .Prefix = ""
161- } else if n := node .Parent ; n != nil && node .NamespaceURI == n .NamespaceURI {
173+ } else if n := node .Parent ; n != nil && ! keepPrefix && node .NamespaceURI == n .NamespaceURI {
162174 node .Prefix = n .Prefix
163175 }
164176 }
0 commit comments