forked from droyo/go-xml
/
search.go
66 lines (57 loc) · 1.35 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package xsd
import "actshad.dev/go-xml/xmltree"
// Search predicates for the xmltree.Element.Search method
type predicate func(el *xmltree.Element) bool
func and(fns ...func(el *xmltree.Element) bool) predicate {
return func(el *xmltree.Element) bool {
for _, f := range fns {
if !f(el) {
return false
}
}
return true
}
}
func or(fns ...func(el *xmltree.Element) bool) predicate {
return func(el *xmltree.Element) bool {
for _, f := range fns {
if f(el) {
return true
}
}
return false
}
}
func hasChild(fn predicate) predicate {
return func(el *xmltree.Element) bool {
for i := range el.Children {
if fn(&el.Children[i]) {
return true
}
}
return false
}
}
func isElem(space, local string) predicate {
return func(el *xmltree.Element) bool {
if el.Name.Local != local {
return false
}
return space == "" || el.Name.Space == space
}
}
func hasAttr(space, local string) predicate {
return func(el *xmltree.Element) bool {
return el.Attr(space, local) != ""
}
}
func hasAttrValue(space, local, value string) predicate {
return func(el *xmltree.Element) bool {
return el.Attr(space, local) == value
}
}
var (
isType = or(isElem(schemaNS, "complexType"), isElem(schemaNS, "simpleType"))
isAnonymousType = and(isType, hasAttrValue("", "name", ""))
hasAnonymousType = hasChild(isAnonymousType)
)