From 88ce6ffcbee250e9c30db69491eb0d2f4c56abef Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sun, 13 Nov 2022 11:13:05 +1100 Subject: [PATCH] Hotfix: actually update the default xml attribute prefix #1428 --- acceptance_tests/inputs-format.sh | 10 ++--- acceptance_tests/output-format.sh | 2 +- cmd/root.go | 18 ++++---- pkg/yqlib/doc/usage/xml.md | 70 +++++++++++++++---------------- pkg/yqlib/xml.go | 2 +- pkg/yqlib/xml_test.go | 61 +++++++++++++++++++-------- release_notes.txt | 3 ++ 7 files changed, 97 insertions(+), 69 deletions(-) diff --git a/acceptance_tests/inputs-format.sh b/acceptance_tests/inputs-format.sh index 8979d04d..2eebd501 100755 --- a/acceptance_tests/inputs-format.sh +++ b/acceptance_tests/inputs-format.sh @@ -109,7 +109,7 @@ EOL read -r -d '' expected << EOM cat: +content: BiBi - +legs: "4" + +@legs: "4" EOM X=$(./yq e -p=xml test.yml) @@ -129,9 +129,9 @@ EOL read -r -d '' expected << EOM +p_xml: version="1.0" map: - +xmlns: some-namespace - +xmlns:xsi: some-instance - +xsi:schemaLocation: some-url + +@xmlns: some-namespace + +@xmlns:xsi: some-instance + +@xsi:schemaLocation: some-url EOM X=$(./yq e -p=xml test.yml) @@ -190,7 +190,7 @@ EOL read -r -d '' expected << EOM cat: +content: BiBi - +legs: "4" + +@legs: "4" EOM X=$(cat /dev/null | ./yq e -p=xml test.yml) diff --git a/acceptance_tests/output-format.sh b/acceptance_tests/output-format.sh index 400668db..268185d4 100755 --- a/acceptance_tests/output-format.sh +++ b/acceptance_tests/output-format.sh @@ -252,7 +252,7 @@ EOM testOutputXmComplex() { cat >test.yml <)") - rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.DirectiveName, "xml-directive-name", "+directive", "name for xml directives (e.g. )") - rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", false, "skip over process instructions (e.g. )") - rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", false, "skip over directives (e.g. )") + rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.AttributePrefix, "xml-attribute-prefix", yqlib.ConfiguredXMLPreferences.AttributePrefix, "prefix for xml attributes") + rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", yqlib.ConfiguredXMLPreferences.ContentName, "name for xml content (if no attribute name is present).") + 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.") + rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.KeepNamespace, "xml-keep-namespace", yqlib.ConfiguredXMLPreferences.KeepNamespace, "enables keeping namespace after parsing attributes") + 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.") + rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "xml-proc-inst-prefix", yqlib.ConfiguredXMLPreferences.ProcInstPrefix, "prefix for xml processing instructions (e.g. )") + rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.DirectiveName, "xml-directive-name", yqlib.ConfiguredXMLPreferences.DirectiveName, "name for xml directives (e.g. )") + rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", yqlib.ConfiguredXMLPreferences.SkipProcInst, "skip over process instructions (e.g. )") + rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. )") rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.") rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)") diff --git a/pkg/yqlib/doc/usage/xml.md b/pkg/yqlib/doc/usage/xml.md index 11c4193e..09f77b09 100644 --- a/pkg/yqlib/doc/usage/xml.md +++ b/pkg/yqlib/doc/usage/xml.md @@ -258,55 +258,55 @@ cat: ``` ## Parse xml: keep attribute namespace +Defaults to true + Given a sample.xml file of: ```xml - - - + ``` then ```bash -yq -p=xml -o=xml --xml-keep-namespace '.' sample.xml +yq -p=xml -o=xml --xml-keep-namespace=false '.' sample.xml ``` will output ```xml - -``` - -instead of -```xml - - -``` - -## Parse xml: keep raw attribute namespace -Given a sample.xml file of: -```xml - - - - - -``` -then -```bash -yq -p=xml -o=xml --xml-keep-namespace --xml-raw-token '.' sample.xml -``` -will output -```xml - - -``` - -instead of -```xml - ``` +instead of +```xml + + +``` + +## Parse xml: keep raw attribute namespace +Defaults to true + +Given a sample.xml file of: +```xml + + + +``` +then +```bash +yq -p=xml -o=xml --xml-raw-token=false '.' sample.xml +``` +will output +```xml + + +``` + +instead of +```xml + + +``` + ## Encode xml: simple Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/xml.go b/pkg/yqlib/xml.go index 55e384ac..09bb872c 100644 --- a/pkg/yqlib/xml.go +++ b/pkg/yqlib/xml.go @@ -18,7 +18,7 @@ func NewDefaultXmlPreferences() XmlPreferences { ContentName: "+content", StrictMode: false, KeepNamespace: true, - UseRawToken: false, + UseRawToken: true, ProcInstPrefix: "+p_", DirectiveName: "+directive", SkipProcInst: false, diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 57ba56c1..be553253 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -157,17 +157,15 @@ const expectedXMLWithComments = ` ` -const inputXMLWithNamespacedAttr = ` - - - +const inputXMLWithNamespacedAttr = ` + ` const expectedYAMLWithNamespacedAttr = `+p_xml: version="1.0" map: +@xmlns: some-namespace +@xmlns:xsi: some-instance - +@some-instance:schemaLocation: some-url + +@xsi:schemaLocation: some-url ` const expectedYAMLWithRawNamespacedAttr = `+p_xml: version="1.0" @@ -177,6 +175,13 @@ map: +@xsi:schemaLocation: some-url ` +const expectedYAMLWithoutRawNamespacedAttr = `+p_xml: version="1.0" +map: + +@xmlns: some-namespace + +@xmlns:xsi: some-instance + +@some-instance:schemaLocation: some-url +` + const xmlWithCustomDtd = `