Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform.Unmarshal for XML docs needs clarification #1622

Closed
faraixyz opened this issue Jan 4, 2022 · 3 comments · Fixed by #2296
Closed

transform.Unmarshal for XML docs needs clarification #1622

faraixyz opened this issue Jan 4, 2022 · 3 comments · Fixed by #2296

Comments

@faraixyz
Copy link
Contributor

faraixyz commented Jan 4, 2022

I'm excited for the XML support but I was wondering how things like attributes, tags with a namespace and multiple tags of the same type are handled as they aren't covered in the docs . From the little work I've done, I've found that

  • multiple tags of the same name return a map with each instance,
  • attributes start with a -. Given that, you need to use the index function,
  • it looks like the namespaces are stripped out.
@faraixyz faraixyz changed the title transform.Unmarshal for XML needs clarification transform.Unmarshal for XML docs needs clarification Jan 4, 2022
@jmooring
Copy link
Member

jmooring commented Jan 4, 2022

Please provide a small example with attributes, tags with a namespace, and multiple tags of the same type.

@faraixyz
Copy link
Contributor Author

faraixyz commented Jan 4, 2022

With an RSS XML feed like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>RSS Feed</title>
        <link>/</link>
        <atom:id>tag:an.example,2020-01-10:/</atom:id>
        <atom:link href="/feed.rss.xml" rel="self" type="application/rss+xml" />
        <item>
            <title>A</title>
            <link>/a</link>
            <guid>/a</guid>
            <description>Some Stuff</description>
        </item>
        <item>
            <title>B</title>
            <link>/b</link>
            <guid>/b</guid>
            <description>Some More Stuff</description>
        </item>
        <item>
            <title>C</title>
            <link>/c</link>
            <guid>/c</guid>
            <description>Some Stuff Again</description>
        </item>
    </channel>
</rss>

This is dumped

map[-atom:http://www.w3.org/2005/Atom -version:2.0 channel:map[item:[map[description:Some Stuff guid:/a link:/a title:A] map[description:Some More Stuff guid:/b link:/b title:B] map[description:Some Stuff Again guid:/c link:/c title:C]] link:[/ map[-href:/feed.rss.xml -rel:self -type:application/rss+xml]] title:RSS Feed]] 

Accessing things will go like this

{{ $feed.-atom }} => error
{{ index $feed "-atom" }} => http://www.w3.org/2005/Atom
{{ $feed.channel.title }} => RSS Feed
{{ $feed.channel.item }} => [map[...] map[...] map[...]]
{{ $feed.channel.id }} => tag:an.example,2020-01-10:/
{{ index $feed.channel "atom:id" }} => 
{{ index $feed.channel "id" }} => tag:an.example,2020-01-10:/
{{ $feed.link }} => [/ map[-href:/feed.rss.xml -rel:self -type:application/rss+xml]] 

@wgroeneveld
Copy link
Contributor

@jmooring led me here via https://discourse.gohugo.io/t/parsing-xml-in-data-with-hugos-new-xml-support-attributes/36654/2 -- I discovered the same shortcomings in the docs.
Here's my attempt to fix it: #1637

jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 15, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 16, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 19, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 19, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 20, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 24, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 27, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Oct 30, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 1, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 2, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 2, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 2, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 2, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 2, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 2, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 3, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 3, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 3, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 3, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 4, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 4, 2023
jmooring added a commit to jmooring/hugo-docs that referenced this issue Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants