how to write self-closing xml tags

4,147 views
Skip to first unread message

Sven Engelhardt

unread,
May 8, 2012, 6:19:38 PM5/8/12
to golang-nuts
I need to write XML documents that should contain self-closing tags -
like the <male/> tag in the example below.
I was wondering if there is some easy way to do this with encoding/xml Marshal?

<person>
<name>John Doe</name>
<male/>
</person>

thanks in advance,

Sven

Kyle Lemons

unread,
May 8, 2012, 8:02:17 PM5/8/12
to Sven Engelhardt, golang-nuts
I considered it when I wrote xml.Marshal, but the prevailing notion became that there was little benefit, since <male></male> should be the same semantically.

Kevin Ballard

unread,
May 8, 2012, 8:23:14 PM5/8/12
to Sven Engelhardt, golang-nuts
Is there some reason you need self-closing tags? XML parsers should treat <male/> and <male></male> identically (e.g. stream-based parsers should emit an open tag event followed by a close tag event in both cases, and DOM-based parsers should generate identical nodes).

-Kevin

Sven Engelhardt

unread,
May 9, 2012, 11:54:27 AM5/9/12
to kbal...@gmail.com, Kyle Lemons, golang-nuts
Kyle, Kevin,

I actually need that to place parametrized RPC calls and that
particular parser on the far end is a slightly picky about what it
gets (and yes, I know that their dialect is not actually XML).

Besides, sometimes the lack of self-closing tags just produces ugly results:

<img src="/foo.png"></img>
<br></br>

or in my case

<request-information>
<detailed></detailed>
</request-information>

Anyway, thanks for looking into this.

-Sven

2012/5/9 Kevin Ballard <kbal...@gmail.com>:

Russ Cox

unread,
May 9, 2012, 12:01:23 PM5/9/12
to Sven Engelhardt, kbal...@gmail.com, Kyle Lemons, golang-nuts
strings.Replace(s, "<detailed></detailed>", "<detailed/>", -1)

Glenn Brown

unread,
May 9, 2012, 12:23:12 PM5/9/12
to Sven Engelhardt, Glenn Brown, kbal...@gmail.com, Kyle Lemons, golang-nuts
I suggest creating a custom Writer to post-process the XML stream to merge <X></X> into <X/>. Conceptually:
$ echo '<_F00></_F00>' | sed -e 's@<\([^>]*\)></\1>@<\1/>@g'
<_F00/>
However, done right, you need to worry about XML quoting, in case you run across
<X Y="<_F00></_F00>">
Also, Go regexp does not support back references, so don't try the regular expression above with it.

--Glenn

Henrik Johansson

unread,
Jan 30, 2017, 11:06:49 AM1/30/17
to golang-nuts, sv...@engelhardt.to, kev...@google.com
I am truly sorry for resurrecting this old thread but I found my self needing to emit xml containing:

<a id="an_id" another_attr="some_value" /> 

today and I am unsure how to deal with it.

I have to do it for legacy reasons and there is very little leeway in what the clients can accept.
It is also high volumes and no files are generated so post processing using files is not really an option.

Any ideas how to do this? Is it perhaps solved since this time but the xml has so little use that it's not even googlable?

Thx,

C Banning

unread,
Jan 30, 2017, 11:53:57 AM1/30/17
to golang-nuts, sv...@engelhardt.to, kev...@google.com

Henrik Johansson

unread,
Jan 30, 2017, 11:57:49 AM1/30/17
to C Banning, golang-nuts, sv...@engelhardt.to, kev...@google.com
Aha yes well if thats the only way then I guess it could work.

Would anyone object to adding some config for this to Encoder?

Thanks,


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages