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

Fix an issue with namespaces when an element is unmarshalled #32

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1656,18 +1656,24 @@ 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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a new test case for testing this functionality (elements from the default namespace)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for it 👍

for _, attr := range currentElement.Attr {
if attr.Space == "xmlns" || attr.Key == "xmlns" {
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
Loading