{"record":{"id":"32d333034a560765","repo":"gohugoio/hugo","slug":"failed-to-unmarshal-xml-w","errorCode":null,"errorMessage":"failed to unmarshal XML: %w","messagePattern":"failed to unmarshal XML: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"parser/metadecoders/decoder.go","lineNumber":294,"sourceCode":"\n// UnmarshalTo unmarshals data in format f into v.\nfunc (d Decoder) UnmarshalTo(data []byte, f Format, v any) error {\n\tvar err error\n\n\tswitch f {\n\tcase ORG:\n\t\terr = d.unmarshalORG(data, v)\n\tcase JSON:\n\t\terr = json.Unmarshal(data, v)\n\tcase XML:\n\t\tvar xmlRoot xml.Map\n\t\txmlRoot, err = xml.NewMapXml(data)\n\n\t\tvar xmlValue map[string]any\n\t\tif err == nil {\n\t\t\txmlRootName, err := xmlRoot.Root()\n\t\t\tif err != nil {\n\t\t\t\treturn toFileError(f, data, fmt.Errorf(\"failed to unmarshal XML: %w\", err))\n\t\t\t}\n\n\t\t\t// Get the root value and verify it's a map\n\t\t\trootValue := xmlRoot[xmlRootName]\n\t\t\tif rootValue == nil {\n\t\t\t\treturn toFileError(f, data, fmt.Errorf(\"XML root element '%s' has no value\", xmlRootName))\n\t\t\t}\n\n\t\t\t// Type check before conversion\n\t\t\tmapValue, ok := rootValue.(map[string]any)\n\t\t\tif !ok {\n\t\t\t\treturn toFileError(f, data, fmt.Errorf(\"XML root element '%s' must be a map/object, got %T\", xmlRootName, rootValue))\n\t\t\t}\n\t\t\txmlValue = mapValue\n\t\t}\n\n\t\tswitch v := v.(type) {\n\t\tcase *map[string]any:","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/parser/metadecoders/decoder.go#L276-L312","documentation":"Thrown in the XML branch of Decoder.UnmarshalTo (parser/metadecoders/decoder.go:294) when xmlRoot.Root() (from clbanning/mxj) returns an error after parsing. This means the XML could be parsed into a Map but extracting the root element name failed — typically indicating malformed, empty, or non-element XML. The error is wrapped via toFileError to attach file context.","triggerScenarios":"Feeding UnmarshalTo XML data that is a fragment, an XML declaration only (<?xml?> with no root element), mixed/invalid content, or text that mxj parses but cannot identify a single root for; loading a .xml data file with broken structure.","commonSituations":"An XML data file under data/ that is empty or contains only a declaration; an externally-fetched XML feed that is malformed; copy-paste introducing a stray tag; encoding issues (BOM, wrong charset) confusing the parser.","solutions":["Validate the XML is well-formed with a single root element using xmllint or a browser.","Ensure the file has actual element content, not just an <?xml?> declaration.","Re-fetch or re-export the source XML if it is truncated/corrupt.","If the data isn't truly XML, rename/reformat the file to its actual format."],"exampleFix":"<!-- before -->\n<?xml version=\"1.0\"?>\n\n<!-- after -->\n<?xml version=\"1.0\"?>\n<root>\n  <item>value</item>\n</root>","handlingStrategy":"validation","validationCode":"// Basic XML well-formedness check before UnmarshalTo.\nfunc looksLikeXML(data []byte) bool {\n    return bytes.Contains(data, []byte(\"<\")) && bytes.Contains(data, []byte(\">\"))\n}","typeGuard":null,"tryCatchPattern":"if err := dec.UnmarshalTo(data, metadecoders.XML, &m); err != nil {\n    return fmt.Errorf(\"invalid XML data: %w\", err)\n}","preventionTips":["Validate XML with xmllint before placing under data/.","Ensure a single root element with content.","Re-fetch corrupt feeds."],"tags":["xml","metadecoders","data","hugo"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}