diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 365dc27..b7c11a1 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -58,16 +58,7 @@ const ( func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) error { decoder := xml.NewDecoder(reader) decoder.Strict = false - decoder.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) { - if strings.ToLower(charset) == "utf-16" { - charset = "utf-8" - } - e, err := ianaindex.MIME.Encoding(charset) - if err != nil { - return nil, err - } - return transform.NewReader(input, e.NewDecoder()), nil - } + decoder.CharsetReader = getCharsetReader level := 0 hasContent := false @@ -216,7 +207,8 @@ func XPathQuery(reader io.Reader, writer io.Writer, query string, singleNode boo doc, err := xmlquery.ParseWithOptions(reader, xmlquery.ParserOptions{ Decoder: &xmlquery.DecoderOptions{ - Strict: false, + Strict: false, + CharsetReader: getCharsetReader, }, }) if err != nil { @@ -614,3 +606,14 @@ func normalizeSpaces(input string, indent string, level int) string { return input } + +func getCharsetReader(charset string, input io.Reader) (io.Reader, error) { + if strings.ToLower(charset) == "utf-16" { + charset = "utf-8" + } + e, err := ianaindex.MIME.Encoding(charset) + if err != nil { + return nil, err + } + return transform.NewReader(input, e.NewDecoder()), nil +}