elementWithNamespaces returns a copy of el that declares every namespace prefix it inherits from its ancestors.
(el *etree.Element)
| 3830 | // elementWithNamespaces returns a copy of el that declares every namespace |
| 3831 | // prefix it inherits from its ancestors. |
| 3832 | func elementWithNamespaces(el *etree.Element) *etree.Element { |
| 3833 | root := el.Copy() |
| 3834 | |
| 3835 | declared := map[string]bool{} |
| 3836 | for _, attr := range root.Attr { |
| 3837 | if prefix, ok := namespaceDeclPrefix(attr); ok { |
| 3838 | declared[prefix] = true |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | // Walk outward so the nearest declaration of each prefix wins. |
| 3843 | for p := el.Parent(); p != nil; p = p.Parent() { |
| 3844 | for _, attr := range p.Attr { |
| 3845 | prefix, ok := namespaceDeclPrefix(attr) |
| 3846 | if !ok || declared[prefix] { |
| 3847 | continue |
| 3848 | } |
| 3849 | declared[prefix] = true |
| 3850 | root.CreateAttr(attr.FullKey(), attr.Value) |
| 3851 | } |
| 3852 | } |
| 3853 | return root |
| 3854 | } |
| 3855 | |
| 3856 | func elementCopyToBytes(root *etree.Element) ([]byte, error) { |
| 3857 | doc := etree.NewDocument() |
no test coverage detected