From f3538850f2e223f9ae2b99f7ed21503416273802 Mon Sep 17 00:00:00 2001 From: Bao Trinh Date: Wed, 1 Oct 2025 16:48:03 -0500 Subject: [PATCH] fix: keep xml namespace prefixes for tags --- pkg/yqlib/decoder_xml.go | 8 +++++++- pkg/yqlib/doc/usage/xml.md | 12 ++++++------ pkg/yqlib/xml_test.go | 22 +++++++++++----------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/pkg/yqlib/decoder_xml.go b/pkg/yqlib/decoder_xml.go index 6759c9cd..febf112f 100644 --- a/pkg/yqlib/decoder_xml.go +++ b/pkg/yqlib/decoder_xml.go @@ -270,10 +270,16 @@ func (dec *xmlDecoder) decodeXML(root *xmlNode) error { log.Debug("start element %v", se.Name.Local) elem.state = "started" // Build new a new current element and link it to its parent + var label = se.Name.Local + if dec.prefs.KeepNamespace { + if se.Name.Space != "" { + label = se.Name.Space + ":" + se.Name.Local + } + } elem = &element{ parent: elem, n: &xmlNode{}, - label: se.Name.Local, + label: label, } // Extract attributes as children diff --git a/pkg/yqlib/doc/usage/xml.md b/pkg/yqlib/doc/usage/xml.md index 30349f06..c5bb6bc5 100644 --- a/pkg/yqlib/doc/usage/xml.md +++ b/pkg/yqlib/doc/usage/xml.md @@ -343,7 +343,7 @@ instead of baz - foobar + foobar ``` @@ -366,10 +366,10 @@ yq --xml-raw-token=false '.' sample.xml will output ```xml - - baz - foobar - + + baz + foobar + ``` instead of @@ -377,7 +377,7 @@ instead of baz - foobar + foobar ``` diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index f17d5581..87ad4301 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -200,9 +200,9 @@ map: +@xmlns:xsi: some-instance +@xsi:schemaLocation: some-url item: - - +content: baz - +@foo: bar - - foobar + +content: baz + +@foo: bar + xsi:item: foobar ` const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0" @@ -211,20 +211,20 @@ map: +@xmlns:xsi: some-instance +@xsi:schemaLocation: some-url item: - - +content: baz - +@foo: bar - - foobar + +content: baz + +@foo: bar + xsi:item: foobar ` const expectedYAMLWithoutRawNamespacedAttr = `+p_xml: version="1.0" -map: +some-namespace:map: +@xmlns: some-namespace +@xmlns:xsi: some-instance +@some-instance:schemaLocation: some-url - item: - - +content: baz - +@foo: bar - - foobar + some-namespace:item: + +content: baz + +@foo: bar + some-instance:item: foobar ` const xmlWithCustomDtd = `