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

encoding/xml: allow Marshal to use CDATA #12963

ghost opened this Issue Oct 16, 2015 · 4 comments


None yet
3 participants
Copy link

ghost commented Oct 16, 2015

My team is interested in generating VAST 3 (video ad standard template) XML using the xml package. Unfortunately the VAST 3 spec specifies:

" All URIs or any other free text fields containing potentially dangerous characters contained in the VAST document should be wrapped in CDATA blocks." p. 23

Currently fields labeled with the xml ",chardata" tag are only output as URL encoded XML. We have a workaround using the Marshaler interface, but it's verbose and bug prone. i.e:

// CharData is a string that should be cdata-wrapped according to the VAST 3 spec.
type CharData string

// MarshalXML implements the xml.Marshaler interface for generating XML.
func (cd CharData) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    return e.EncodeElement(struct {
        S string `xml:",innerxml"`
        S: "<![CDATA[" + string(cd) + "]]>",
    }, start)

There should be a first-class way to output CDATA escaped data in the xml package, perhaps with the addition of a ",cdata" tag.

@rsc rsc added this to the Go1.6 milestone Oct 16, 2015


This comment has been minimized.

Copy link

gopherbot commented Oct 18, 2015

CL mentions this issue.


This comment has been minimized.

Copy link

rsc commented Oct 23, 2015

As far as addressing VAST 3 is concerned, does it make more sense to have a ,cdata field or to have a method call on *xml.Encoder that says "use CDATA always"?

@rsc rsc changed the title encoding/xml: Add CDATA-wrapper support to xml.Marshal encoding/xml: allow Marshal to use CDATA Nov 5, 2015

@rsc rsc closed this in 3f6b91b Nov 25, 2015


This comment has been minimized.

Copy link

gopherbot commented Nov 25, 2015

CL mentions this issue.

rsc added a commit that referenced this issue Dec 3, 2015

encoding/xml: document new chardata tag
Followup to CL 16047.
For #12963.

Change-Id: I596cd5109b25a4079b966427411860fde8b9b54a
Reviewed-by: David Crawshaw <>

This comment has been minimized.

Copy link

akrylysov commented Jun 27, 2016

It would be nice if it was a way to put a cdata content into a nested node: allow tags like xml:"a,cdata", xml:"a>b>c,cdata".

@golang golang locked and limited conversation to collaborators Jun 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.