Skip to content

Commit ce9701c

Browse files
committed
Hotfix: actually update the default xml attribute prefix
1 parent 83ef7ee commit ce9701c

File tree

7 files changed

+73
-45
lines changed

7 files changed

+73
-45
lines changed

acceptance_tests/inputs-format.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ EOL
109109
read -r -d '' expected << EOM
110110
cat:
111111
+content: BiBi
112-
+legs: "4"
112+
+@legs: "4"
113113
EOM
114114

115115
X=$(./yq e -p=xml test.yml)
@@ -129,9 +129,9 @@ EOL
129129
read -r -d '' expected << EOM
130130
+p_xml: version="1.0"
131131
map:
132-
+xmlns: some-namespace
133-
+xmlns:xsi: some-instance
134-
+xsi:schemaLocation: some-url
132+
+@xmlns: some-namespace
133+
+@xmlns:xsi: some-instance
134+
+@xsi:schemaLocation: some-url
135135
EOM
136136

137137
X=$(./yq e -p=xml test.yml)
@@ -190,7 +190,7 @@ EOL
190190
read -r -d '' expected << EOM
191191
cat:
192192
+content: BiBi
193-
+legs: "4"
193+
+@legs: "4"
194194
EOM
195195

196196
X=$(cat /dev/null | ./yq e -p=xml test.yml)

acceptance_tests/output-format.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ EOM
252252

253253
testOutputXmComplex() {
254254
cat >test.yml <<EOL
255-
a: {b: {c: ["cat", "dog"], +f: meow}}
255+
a: {b: {c: ["cat", "dog"], +@f: meow}}
256256
EOL
257257

258258
read -r -d '' expected << EOM

cmd/root.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ yq -P sample.json
103103
rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "yaml", "[yaml|y|json|j|props|p|xml|x] output format type.")
104104
rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "yaml", "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml.")
105105

106-
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.AttributePrefix, "xml-attribute-prefix", "+", "prefix for xml attributes")
107-
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", "+content", "name for xml content (if no attribute name is present).")
108-
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.StrictMode, "xml-strict-mode", false, "enables strict parsing of XML. See https://pkg.go.dev/encoding/xml for more details.")
109-
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.KeepNamespace, "xml-keep-namespace", true, "enables keeping namespace after parsing attributes")
110-
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.UseRawToken, "xml-raw-token", true, "enables using RawToken method instead Token. Commonly disables namespace translations. See https://pkg.go.dev/encoding/xml#Decoder.RawToken for details.")
111-
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "xml-proc-inst-prefix", "+p_", "prefix for xml processing instructions (e.g. <?xml version=\"1\"?>)")
112-
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.DirectiveName, "xml-directive-name", "+directive", "name for xml directives (e.g. <!DOCTYPE thing cat>)")
113-
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", false, "skip over process instructions (e.g. <?xml version=\"1\"?>)")
114-
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", false, "skip over directives (e.g. <!DOCTYPE thing cat>)")
106+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.AttributePrefix, "xml-attribute-prefix", yqlib.ConfiguredXMLPreferences.AttributePrefix, "prefix for xml attributes")
107+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", yqlib.ConfiguredXMLPreferences.ContentName, "name for xml content (if no attribute name is present).")
108+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.StrictMode, "xml-strict-mode", yqlib.ConfiguredXMLPreferences.StrictMode, "enables strict parsing of XML. See https://pkg.go.dev/encoding/xml for more details.")
109+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.KeepNamespace, "xml-keep-namespace", yqlib.ConfiguredXMLPreferences.KeepNamespace, "enables keeping namespace after parsing attributes")
110+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.UseRawToken, "xml-raw-token", yqlib.ConfiguredXMLPreferences.UseRawToken, "enables using RawToken method instead Token. Commonly disables namespace translations. See https://pkg.go.dev/encoding/xml#Decoder.RawToken for details.")
111+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "xml-proc-inst-prefix", yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "prefix for xml processing instructions (e.g. <?xml version=\"1\"?>)")
112+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.DirectiveName, "xml-directive-name", yqlib.ConfiguredXMLPreferences.DirectiveName, "name for xml directives (e.g. <!DOCTYPE thing cat>)")
113+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", yqlib.ConfiguredXMLPreferences.SkipProcInst, "skip over process instructions (e.g. <?xml version=\"1\"?>)")
114+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. <!DOCTYPE thing cat>)")
115115

116116
rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.")
117117
rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)")

pkg/yqlib/doc/usage/xml.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,42 +258,42 @@ cat:
258258
```
259259

260260
## Parse xml: keep attribute namespace
261+
Defaults to true
262+
261263
Given a sample.xml file of:
262264
```xml
263-
264265
<?xml version="1.0"?>
265-
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
266-
</map>
266+
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
267267
268268
```
269269
then
270270
```bash
271-
yq -p=xml -o=xml --xml-keep-namespace '.' sample.xml
271+
yq -p=xml -o=xml --xml-keep-namespace=false '.' sample.xml
272272
```
273273
will output
274274
```xml
275275
<?xml version="1.0"?>
276-
<map xmlns="some-namespace" xmlns:xsi="some-instance" some-instance:schemaLocation="some-url"></map>
276+
<map xmlns="some-namespace" xsi="some-instance" schemaLocation="some-url"></map>
277277
```
278278

279279
instead of
280280
```xml
281281
<?xml version="1.0"?>
282-
<map xmlns="some-namespace" xmlns:xsi="some-instance" some-instance:schemaLocation="some-url"></map>
282+
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
283283
```
284284

285285
## Parse xml: keep raw attribute namespace
286+
Defaults to true
287+
286288
Given a sample.xml file of:
287289
```xml
288-
289290
<?xml version="1.0"?>
290-
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
291-
</map>
291+
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
292292
293293
```
294294
then
295295
```bash
296-
yq -p=xml -o=xml --xml-keep-namespace --xml-raw-token '.' sample.xml
296+
yq -p=xml -o=xml --xml-raw-token=false '.' sample.xml
297297
```
298298
will output
299299
```xml
@@ -304,7 +304,7 @@ will output
304304
instead of
305305
```xml
306306
<?xml version="1.0"?>
307-
<map xmlns="some-namespace" xsi="some-instance" schemaLocation="some-url"></map>
307+
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
308308
```
309309

310310
## Encode xml: simple

pkg/yqlib/xml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func NewDefaultXmlPreferences() XmlPreferences {
1818
ContentName: "+content",
1919
StrictMode: false,
2020
KeepNamespace: true,
21-
UseRawToken: false,
21+
UseRawToken: true,
2222
ProcInstPrefix: "+p_",
2323
DirectiveName: "+directive",
2424
SkipProcInst: false,

pkg/yqlib/xml_test.go

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,15 @@ const expectedXMLWithComments = `<!--
157157
</cat><!-- below_cat -->
158158
`
159159

160-
const inputXMLWithNamespacedAttr = `
161-
<?xml version="1.0"?>
162-
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
163-
</map>
160+
const inputXMLWithNamespacedAttr = `<?xml version="1.0"?>
161+
<map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url"></map>
164162
`
165163

166164
const expectedYAMLWithNamespacedAttr = `+p_xml: version="1.0"
167165
map:
168166
+@xmlns: some-namespace
169167
+@xmlns:xsi: some-instance
170-
+@some-instance:schemaLocation: some-url
168+
+@xsi:schemaLocation: some-url
171169
`
172170

173171
const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0"
@@ -177,6 +175,13 @@ map:
177175
+@xsi:schemaLocation: some-url
178176
`
179177

178+
const expectedYAMLWithoutRawNamespacedAttr = `+p_xml: version="1.0"
179+
map:
180+
+@xmlns: some-namespace
181+
+@xmlns:xsi: some-instance
182+
+@some-instance:schemaLocation: some-url
183+
`
184+
180185
const xmlWithCustomDtd = `
181186
<?xml version="1.0"?>
182187
<!DOCTYPE root [
@@ -266,6 +271,13 @@ var xmlScenarios = []formatScenario{
266271
expected: expectedDtd,
267272
scenarioType: "roundtrip",
268273
},
274+
{
275+
description: "Roundtrip with name spaced attributes",
276+
skipDoc: true,
277+
input: inputXMLWithNamespacedAttr,
278+
expected: inputXMLWithNamespacedAttr,
279+
scenarioType: "roundtrip",
280+
},
269281
{
270282
description: "Parse xml: skip custom dtd",
271283
subdescription: "DTDs are directives, skip over directives to skip DTDs.",
@@ -323,19 +335,28 @@ var xmlScenarios = []formatScenario{
323335
scenarioType: "decode",
324336
},
325337
{
326-
description: "Parse xml: keep attribute namespace",
327-
skipDoc: false,
328-
input: inputXMLWithNamespacedAttr,
329-
expected: expectedYAMLWithNamespacedAttr,
330-
scenarioType: "decode-keep-ns",
338+
description: "Parse xml: keep attribute namespace",
339+
subdescription: fmt.Sprintf(`Defaults to %v`, ConfiguredXMLPreferences.KeepNamespace),
340+
skipDoc: false,
341+
input: inputXMLWithNamespacedAttr,
342+
expected: expectedYAMLWithNamespacedAttr,
343+
scenarioType: "decode-keep-ns",
331344
},
332345
{
333346
description: "Parse xml: keep raw attribute namespace",
334-
skipDoc: false,
347+
skipDoc: true,
335348
input: inputXMLWithNamespacedAttr,
336349
expected: expectedYAMLWithRawNamespacedAttr,
337350
scenarioType: "decode-raw-token",
338351
},
352+
{
353+
description: "Parse xml: keep raw attribute namespace",
354+
subdescription: fmt.Sprintf(`Defaults to %v`, ConfiguredXMLPreferences.UseRawToken),
355+
skipDoc: false,
356+
input: inputXMLWithNamespacedAttr,
357+
expected: expectedYAMLWithoutRawNamespacedAttr,
358+
scenarioType: "decode-raw-token-off",
359+
},
339360
{
340361
description: "Encode xml: simple",
341362
input: "cat: purrs",
@@ -452,6 +473,10 @@ func testXMLScenario(t *testing.T, s formatScenario) {
452473
prefs := NewDefaultXmlPreferences()
453474
prefs.UseRawToken = true
454475
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
476+
case "decode-raw-token-off":
477+
prefs := NewDefaultXmlPreferences()
478+
prefs.UseRawToken = false
479+
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewYamlEncoder(2, false, ConfiguredYamlPreferences)), s.description)
455480
case "roundtrip-skip-directives":
456481
prefs := NewDefaultXmlPreferences()
457482
prefs.SkipDirectives = true
@@ -484,7 +509,7 @@ func documentXMLScenario(t *testing.T, w *bufio.Writer, i interface{}) {
484509
documentXMLRoundTripScenario(w, s)
485510
case "decode-keep-ns":
486511
documentXMLDecodeKeepNsScenario(w, s)
487-
case "decode-raw-token":
512+
case "decode-raw-token-off":
488513
documentXMLDecodeKeepNsRawTokenScenario(w, s)
489514
case "roundtrip-skip-directives":
490515
documentXMLSkipDirectrivesScenario(w, s)
@@ -528,14 +553,14 @@ func documentXMLDecodeKeepNsScenario(w *bufio.Writer, s formatScenario) {
528553
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input))
529554

530555
writeOrPanic(w, "then\n")
531-
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace '.' sample.xml\n```\n")
556+
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace=false '.' sample.xml\n```\n")
532557
writeOrPanic(w, "will output\n")
533558
prefs := NewDefaultXmlPreferences()
534-
prefs.KeepNamespace = true
559+
prefs.KeepNamespace = false
535560
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs))))
536561

537562
prefsWithout := NewDefaultXmlPreferences()
538-
prefs.KeepNamespace = false
563+
prefs.KeepNamespace = true
539564
writeOrPanic(w, "instead of\n")
540565
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefsWithout), NewXMLEncoder(2, prefsWithout))))
541566
}
@@ -552,16 +577,16 @@ func documentXMLDecodeKeepNsRawTokenScenario(w *bufio.Writer, s formatScenario)
552577
writeOrPanic(w, fmt.Sprintf("```xml\n%v\n```\n", s.input))
553578

554579
writeOrPanic(w, "then\n")
555-
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-keep-namespace --xml-raw-token '.' sample.xml\n```\n")
580+
writeOrPanic(w, "```bash\nyq -p=xml -o=xml --xml-raw-token=false '.' sample.xml\n```\n")
556581
writeOrPanic(w, "will output\n")
557582

558583
prefs := NewDefaultXmlPreferences()
559-
prefs.KeepNamespace = true
584+
prefs.UseRawToken = false
560585

561586
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs))))
562587

563588
prefsWithout := NewDefaultXmlPreferences()
564-
prefsWithout.KeepNamespace = false
589+
prefsWithout.UseRawToken = true
565590

566591
writeOrPanic(w, "instead of\n")
567592
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", mustProcessFormatScenario(s, NewXMLDecoder(prefsWithout), NewXMLEncoder(2, prefsWithout))))

release_notes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
4.30.2:
2+
- Actually updated the default xml prefix :facepalm:
3+
14
4.30.1:
25
- XML users note: the default attribute prefix has change to `+@` to avoid naming conflicts!
36
- Can use expressions in slice #1419

0 commit comments

Comments
 (0)