{"record":{"id":"b597432e97864382","repo":"gohugoio/hugo","slug":"xml-root-element-s-has-no-value","errorCode":null,"errorMessage":"XML root element '%s' has no value","messagePattern":"XML root element '(.+?)' has no value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"parser/metadecoders/decoder.go","lineNumber":300,"sourceCode":"\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:\n\t\t\t*v = xmlValue\n\t\tcase *any:\n\t\t\t*v = xmlValue\n\t\t}\n\tcase TOML:\n\t\terr = toml.Unmarshal(data, v)","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/parser/metadecoders/decoder.go#L282-L318","documentation":"Thrown in the XML branch of Decoder.UnmarshalTo (parser/metadecoders/decoder.go:300) when the XML root element parses successfully and has a name, but its value is nil (the root element is empty or self-closing with no children/text). Hugo requires the root to carry a value, so an empty <root/> triggers this error naming the element.","triggerScenarios":"An XML data file whose root element is empty: <root></root> or <root/>; an element that contains only attributes but no child elements or text, which mxj represents as nil.","commonSituations":"Exporting an empty dataset to XML; a feed endpoint returning an empty container; a templating tool emitting a self-closing root.","solutions":["Populate the root element with at least one child element or text value.","If empty data is legitimate, handle the empty-data case upstream before feeding it to Hugo.","Switch to JSON if the XML is always a flat key/value container."],"exampleFix":"<!-- before -->\n<root/>\n\n<!-- after -->\n<root>\n  <item>value</item>\n</root>","handlingStrategy":"validation","validationCode":"// Ensure XML root has content before decoding.\nfunc xmlRootHasContent(data []byte) bool {\n    return bytes.Count(data, []byte(\"<\")) > 1\n}","typeGuard":null,"tryCatchPattern":"if err := dec.UnmarshalTo(data, metadecoders.XML, &m); err != nil {\n    return fmt.Errorf(\"empty XML root: %w\", err)\n}","preventionTips":["Populate root element with child elements or text.","Handle empty datasets upstream.","Use JSON for simple flat containers."],"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"}