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: ability to add attributes #4169

Closed
lukescott opened this issue Sep 28, 2012 · 8 comments
Closed

encoding/xml: ability to add attributes #4169

lukescott opened this issue Sep 28, 2012 · 8 comments
Milestone

Comments

@lukescott
Copy link

Using this code:

http://play.golang.org/p/NDaijJakYp

I get this:

<ns1:runTransaction>
    <Token>
        <ClientIP>10.1.10.1</ClientIP>
        <PinHash>
            <HashValue>6f06b252a955f85590d8c08adaaaad73</HashValue>
            <Type>md5</Type>
            <Seed>13486807611753321939</Seed>
        </PinHash>
        <SourceKey>Zz85lB6J8VkleGr6W6BijBkzz2N3ZVky</SourceKey>
    </Token>
</ns1:runTransaction>

I need it to look like this:

<ns1:runTransaction>
    <Token xsi:type="ns1:ueSecurityToken">
        <ClientIP xsi:type="xsd:string">10.1.10.1</ClientIP>
        <PinHash xsi:type="ns1:ueHash">
            <HashValue xsi:type="xsd:string">6f06b252a955f85590d8c08adaaaad73</HashValue>
            <Type xsi:type="xsd:string">md5</Type>
            <Seed xsi:type="xsd:string">13486807611753321939</Seed>
        </PinHash>
        <SourceKey xsi:type="xsd:string">Zz85lB6J8VkleGr6W6BijBkzz2N3ZVky</SourceKey>
    </Token>
</ns1:runTransaction>

Notice in the code linked above I have `xsi:"type"` added to each item in the
struct.

Optionally it would be nice to be able to specify static attributes. But the proposed
functionality is 7 new lines (I've patched a local copy).
@rsc
Copy link
Contributor

rsc commented Oct 6, 2012

Comment 1:

Definitely don't want this on all XML output - I don't want this package to know about
things like "ns1:ueHash" - but maybe we can make it possible to add custom attributes.

Labels changed: added priority-later, go1.1maybe, removed priority-triage.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Mar 12, 2013

Comment 2:

I am sad to say it, but I think we will have to postpone XML work until
after Go 1.1.
I regret that we didn't have more time to make encoding/xml better, but
given the tradeoff I think focusing on core performance and
implementation pieces for this final release push is probably the right
choice. Unlike most of the performance and other stuff we're trying to
shake out right now, functionality such as XML parsing can be provided
by go get-able libraries as a stopgap until Go 1.2.

Labels changed: added go1.2, removed go1.1maybe.

@lukescott
Copy link
Author

Comment 3:

I've written a library called "xmlutil" that allows you to register types, similar to
encoding/gob. So you can literally do something like this:
type Envelope struct {
    Header
    Body
}
type Body struct {
    Response interface{}
}
Any tag (with any name) will be filled into the "Response" interface that is registered.
It takes the tag name in the document and does a name lookup of the type.
In that register function it allows you to add type attributes. Looks something like
this:
xmlutil.RegisterTypeMore(RunTransaction{}, xml.Name{}, []xml.Attr{...});
It does almost the same thing as the encoding/xml marshal/unmarshal, but doesn't support
the ">" tag keyword. I found that the ">" keyword made things really complicated.
Not sure if it would be useful to you guys or not. I hope to open source it soon, just
have to get approval.

@lukescott
Copy link
Author

Comment 4:

This is a package I've created to makeup for missing functionality that I needed:
https://github.com/webconnex/xmlutil
With the opening example in the issue, I can do this:
    x := xmlutil.NewXmlUtil()
    x.RegisterNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi")
    x.RegisterTypeMore("", xml.Name{},
        []xml.Attr{
            xml.Attr{xml.Name{"xsi","type"},"xsd:string"},
        })
And this causes any tags with a string value to have an attribute of
xsi:type="xsd:string"
I built it primarily for use with Soap. It depends on encoding/xml for reading tokens.
It's a bit spartan on some of the Marshal/Unmarshal functionality in encoding/xml.
Specifically it doesn't have parent tags `foo>bar`, and it doesn't have many of the
modifiers (like innerxml). But it does allow you to register types, which is used with
generic interfaces and allows you to add attributes. I hope to have some examples soon.
The type registration is isolated in an instance of "XmlUtil". This allows you to have
specific behaviors with built-in types that you might not want in other parts of your
application. In my case I'm working with multiple SOAP Api's, and not all of them
require/want xsi:type.
Not sure if this is useful to you guys or not. If it is let me know. I haven't added any
licensing yet, so let me know how it should be licensed.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 5:

Labels changed: added feature.

@rsc
Copy link
Contributor

rsc commented Aug 14, 2013

Comment 6:

This issue was closed by revision 85f3acd.

Status changed to Fixed.

@rsc
Copy link
Contributor

rsc commented Aug 14, 2013

Comment 7:

This issue was closed by revision 56ce83f.

@rsc
Copy link
Contributor

rsc commented Aug 14, 2013

Comment 8:

This issue was closed by revision 54bdfc0.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants