mirror of
https://github.com/wader/fq.git
synced 2024-11-23 00:57:15 +03:00
xml: Keep track of default namespace and skip it element names
Refactor element name handling a bit, return it instead, feels nicer.
This commit is contained in:
parent
93acc02e97
commit
f24d685a5b
21
format/xml/testdata/test.svg
vendored
Normal file
21
format/xml/testdata/test.svg
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg
|
||||
xmlns:svg="http://www.w3.org/2000/svg"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
inkscape:version="1.0 (4035a4f, 2020-05-01)"
|
||||
sodipodi:docname="ffclippy.svg"
|
||||
version="1.1"
|
||||
viewBox="0 0 192.756 192.756"
|
||||
height="2500"
|
||||
width="2500">
|
||||
<sodipodi:namedview
|
||||
showgrid="false"
|
||||
pagecolor="#ffffff" />
|
||||
<g
|
||||
id="g863"
|
||||
clip-rule="evenodd"
|
||||
fill-rule="evenodd">
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 599 B |
59
format/xml/testdata/test.svg.fqtest
vendored
Normal file
59
format/xml/testdata/test.svg.fqtest
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
$ fq . test.svg
|
||||
{
|
||||
"svg": {
|
||||
"-height": "2500",
|
||||
"-inkscape:version": "1.0 (4035a4f, 2020-05-01)",
|
||||
"-sodipodi:docname": "ffclippy.svg",
|
||||
"-version": "1.1",
|
||||
"-viewBox": "0 0 192.756 192.756",
|
||||
"-width": "2500",
|
||||
"-xmlns": "http://www.w3.org/2000/svg",
|
||||
"-xmlns:inkscape": "http://www.inkscape.org/namespaces/inkscape",
|
||||
"-xmlns:sodipodi": "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
|
||||
"-xmlns:svg": "http://www.w3.org/2000/svg",
|
||||
"g": {
|
||||
"-clip-rule": "evenodd",
|
||||
"-fill-rule": "evenodd",
|
||||
"-id": "g863"
|
||||
},
|
||||
"sodipodi:namedview": {
|
||||
"-pagecolor": "#ffffff",
|
||||
"-showgrid": "false"
|
||||
}
|
||||
}
|
||||
}
|
||||
$ fq -o array=true . test.svg
|
||||
[
|
||||
"svg",
|
||||
{
|
||||
"height": "2500",
|
||||
"inkscape:version": "1.0 (4035a4f, 2020-05-01)",
|
||||
"sodipodi:docname": "ffclippy.svg",
|
||||
"version": "1.1",
|
||||
"viewBox": "0 0 192.756 192.756",
|
||||
"width": "2500",
|
||||
"xmlns": "http://www.w3.org/2000/svg",
|
||||
"xmlns:inkscape": "http://www.inkscape.org/namespaces/inkscape",
|
||||
"xmlns:sodipodi": "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
|
||||
"xmlns:svg": "http://www.w3.org/2000/svg"
|
||||
},
|
||||
[
|
||||
[
|
||||
"sodipodi:namedview",
|
||||
{
|
||||
"pagecolor": "#ffffff",
|
||||
"showgrid": "false"
|
||||
},
|
||||
[]
|
||||
],
|
||||
[
|
||||
"g",
|
||||
{
|
||||
"clip-rule": "evenodd",
|
||||
"fill-rule": "evenodd",
|
||||
"id": "g863"
|
||||
},
|
||||
[]
|
||||
]
|
||||
]
|
||||
]
|
@ -114,6 +114,9 @@ func fromXMLToArray(n xmlNode) any {
|
||||
space = nss.lookup(a.Name)
|
||||
}
|
||||
name = space + ":" + local
|
||||
} else if local == "xmlns" {
|
||||
// track default namespace
|
||||
nss = nss.push("", a.Value)
|
||||
}
|
||||
attrs[name] = a.Value
|
||||
}
|
||||
@ -133,8 +136,8 @@ func fromXMLToArray(n xmlNode) any {
|
||||
if space != "" {
|
||||
space = nss.lookup(n.XMLName)
|
||||
}
|
||||
// only add if ns is found and not default ns
|
||||
name := elmName(space, local)
|
||||
|
||||
elm := []any{name}
|
||||
if len(attrs) > 0 {
|
||||
elm = append(elm, attrs)
|
||||
@ -151,8 +154,8 @@ func fromXMLToArray(n xmlNode) any {
|
||||
}
|
||||
|
||||
func fromXMLToObject(n xmlNode, xi format.XMLIn) any {
|
||||
var f func(n xmlNode, seq int, nss xmlNNStack) any
|
||||
f = func(n xmlNode, seq int, nss xmlNNStack) any {
|
||||
var f func(n xmlNode, seq int, nss xmlNNStack) (string, any)
|
||||
f = func(n xmlNode, seq int, nss xmlNNStack) (string, any) {
|
||||
attrs := map[string]any{}
|
||||
|
||||
for _, a := range n.Attrs {
|
||||
@ -165,7 +168,11 @@ func fromXMLToObject(n xmlNode, xi format.XMLIn) any {
|
||||
space = nss.lookup(a.Name)
|
||||
}
|
||||
name = space + ":" + local
|
||||
} else if local == "xmlns" {
|
||||
// track default namespace
|
||||
nss = nss.push("", a.Value)
|
||||
}
|
||||
|
||||
attrs["-"+name] = a.Value
|
||||
}
|
||||
|
||||
@ -174,20 +181,17 @@ func fromXMLToObject(n xmlNode, xi format.XMLIn) any {
|
||||
if len(n.Nodes) == 1 {
|
||||
nSeq = -1
|
||||
}
|
||||
local, space := nn.XMLName.Local, nn.XMLName.Space
|
||||
if space != "" {
|
||||
space = nss.lookup(nn.XMLName)
|
||||
}
|
||||
// only add if ns is found and not default ns
|
||||
name := elmName(space, local)
|
||||
if e, ok := attrs[name]; ok {
|
||||
|
||||
nname, naddrs := f(nn, nSeq, nss)
|
||||
|
||||
if e, ok := attrs[nname]; ok {
|
||||
if ea, ok := e.([]any); ok {
|
||||
attrs[name] = append(ea, f(nn, nSeq, nss))
|
||||
attrs[nname] = append(ea, naddrs)
|
||||
} else {
|
||||
attrs[name] = []any{e, f(nn, nSeq, nss)}
|
||||
attrs[nname] = []any{e, naddrs}
|
||||
}
|
||||
} else {
|
||||
attrs[name] = f(nn, nSeq, nss)
|
||||
attrs[nname] = naddrs
|
||||
}
|
||||
}
|
||||
|
||||
@ -201,18 +205,23 @@ func fromXMLToObject(n xmlNode, xi format.XMLIn) any {
|
||||
attrs["#comment"] = strings.TrimSpace(string(n.Comment))
|
||||
}
|
||||
|
||||
local, space := n.XMLName.Local, n.XMLName.Space
|
||||
if space != "" {
|
||||
space = nss.lookup(n.XMLName)
|
||||
}
|
||||
name := elmName(space, local)
|
||||
|
||||
if len(attrs) == 0 {
|
||||
return ""
|
||||
return name, ""
|
||||
} else if len(attrs) == 1 && attrs["#text"] != nil {
|
||||
return attrs["#text"]
|
||||
return name, attrs["#text"]
|
||||
}
|
||||
|
||||
return attrs
|
||||
return name, attrs
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
n.XMLName.Local: f(n, -1, nil),
|
||||
}
|
||||
name, attrs := f(n, -1, nil)
|
||||
return map[string]any{name: attrs}
|
||||
}
|
||||
|
||||
func decodeXML(d *decode.D, in any) any {
|
||||
|
Loading…
Reference in New Issue
Block a user