Skip to content

Commit

Permalink
change how the list of namespaces that apply to an element is constru…
Browse files Browse the repository at this point in the history
…cted
  • Loading branch information
IevaVasiljeva committed Aug 23, 2023
1 parent 67cbfa0 commit 70af345
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1656,18 +1656,25 @@ func findChild(parentEl *etree.Element, childNS string, childTag string) (*etree
}

func elementToBytes(el *etree.Element) ([]byte, error) {
// Retrieve namespaces from the element itself and its parents
namespaces := map[string]string{}
for _, childEl := range el.FindElements("//*") {
ns := childEl.NamespaceURI()
if ns != "" {
namespaces[childEl.Space] = ns
currentElement := el
for currentElement != nil {
for _, attr := range currentElement.Attr {
if attr.Space == "xmlns" || attr.Key == "xmlns" {
fmt.Println("namespace, fuckers")
if _, prefixExists := namespaces[attr.FullKey()]; !prefixExists {
namespaces[attr.FullKey()] = attr.Value
}
}
}
currentElement = currentElement.Parent()
}

doc := etree.NewDocument()
doc.SetRoot(el.Copy())
for space, uri := range namespaces {
doc.Root().CreateAttr("xmlns:"+space, uri)
for prefix, uri := range namespaces {
doc.Root().CreateAttr(prefix, uri)
}

return doc.WriteToBytes()
Expand Down

0 comments on commit 70af345

Please sign in to comment.