Parsing XML in /data with Hugo's new XML support: attributes?

Recently, Hugo received XML data support
See Implement XML data support · gohugoio/hugo@0eaaa8f · GitHub

I’m trying to get it to work. I have a .xml file in my /data folder and want to iterate over tags. That works. For instance, getting the contents of <title>blah</title> indeed is simple: {{ $.Site.Data.myfile.head.title }}.

But what about attributes? Printing the raw value using {{ . }} to peek inside the Go vars gives me something akin to map[-text:Blogging -title:Blogging outline:... for an XML structure like <outline text="Blogging" title="Blogging"><.../>. I want to extract the title and text, and clearly, {{ .text }} does not work: that fetches the contents of the text child, which isn’t there.

The documentation on this shiny new feature I’m afraid is far from complete. No mention anywhere of attributes at transform.Unmarshal | Hugo.

Thanks!

There’s an open docs issue that might be helpful:
https://github.com/gohugoio/hugoDocs/issues/1622

Maybe you can help.

That was exactly what I was looking for, the index fn! Thanks so much!
It is indeed a bit strange that attributes start with -.

I’d love to help with the docs, and I’ve done so before. I’ll add the index information to the page above in a pull request.

Edit: https://github.com/gohugoio/hugoDocs/issues/1622 an attempt to clear … by wgroeneveld · Pull Request #1637 · gohugoio/hugoDocs · GitHub

1 Like