forked from clbanning/x2j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x2jpath_test.go
169 lines (149 loc) · 4.1 KB
/
x2jpath_test.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package x2j
import (
"fmt"
"testing"
)
// the basic demo/test case - a small bibliography with mixed element types
func TestValuesFromTagPath(t *testing.T) {
var doc = `
<doc>
<books>
<book seq="1">
<author>William H. Gaddis</author>
<title>The Recognitions</title>
<review>One of the great seminal American novels of the 20th century.</review>
</book>
<book seq="2">
<author>Austin Tappan Wright</author>
<title>Islandia</title>
<review>An example of earlier 20th century American utopian fiction.</review>
</book>
<book seq="3">
<author>John Hawkes</author>
<title>The Beetle Leg</title>
<review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
</book>
<book seq="4">
<author>
<first_name>T.E.</first_name>
<last_name>Porter</last_name>
</author>
<title>King's Day</title>
<review>A magical novella.</review>
</book>
</books>
</doc>
`
var doc2 = `
<doc>
<books>
<book seq="1">
<author>William H. Gaddis</author>
<title>The Recognitions</title>
<review>One of the great seminal American novels of the 20th century.</review>
</book>
</books>
</doc>
`
fmt.Println("\nTestValuesFromTagPath()\n",doc)
m,_ := DocToMap(doc)
fmt.Println("map:",WriteMap(m),"\n")
v,_ := ValuesFromTagPath(doc,"doc.books")
fmt.Println("path == doc.books: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.books.*")
fmt.Println("path == doc.books.*: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.books.book")
fmt.Println("path == doc.books.book: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc2,"doc.books.book")
fmt.Println("doc == doc2 / path == doc.books.book: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.books.book.*")
fmt.Println("path == doc.books.book.*: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc2,"doc.books.book.*")
fmt.Println("doc == doc2 / path == doc.books.book.*: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.books.*.author")
fmt.Println("path == doc.books.*.author: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.*.*.author")
fmt.Println("path == doc.*.*.author: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.*.*.title")
fmt.Println("path == doc.*.*.title: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.*.*.*")
fmt.Println("path == doc.*.*.*: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
v,_ = ValuesFromTagPath(doc,"doc.*.*.*.*")
fmt.Println("path == doc.*.*.*.*: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val,"\n")
}
}
// demo how to compensate for irregular tag labels in data
// "netid" vs. "idnet"
func TestValuesFromTagPath2(t *testing.T) {
var doc1 = `
<?xml version="1.0" encoding="UTF-8"?>
<data>
<netid>
<disable>no</disable>
<text1>default:text</text1>
<word1>default:word</word1>
</netid>
</data>
`
var doc2 = `
<?xml version="1.0" encoding="UTF-8"?>
<data>
<idnet>
<disable>yes</disable>
<text1>default:text</text1>
<word1>default:word</word1>
</idnet>
</data>
`
var docs = []string{doc1,doc2}
for n,doc := range docs {
fmt.Println("\nTestValuesFromTagPath2(), iteration:",n,"\n",doc)
m,_ := DocToMap(doc)
fmt.Println("map:",WriteMap(m))
v,_ := ValuesFromTagPath(doc,"data.*")
fmt.Println("\npath == data.*: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val)
}
for key,val := range v[0].(map[string]interface{}) {
fmt.Println("\t",key,":",val)
}
v,_ = ValuesFromTagPath(doc,"data.*.*")
fmt.Println("\npath == data.*.*: len(v):",len(v))
for key,val := range v {
fmt.Println(key,":",val)
}
}
}