-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: fix name spaces #13400
Comments
Please don't give up 😄 No doubt that proper namespace handling is a pain, but getting it right is very valuable for those of us who are stuck with legacy APIs that only speak XML. |
Seconding the comment above. Please don't give up on this. There is still value in handling namespaces properly in golang XML. |
I did say we'd try one more time before giving up.
|
+1 I was researching Go as a possible replacement for Python in processing ODT files and I was quickly hitting snags when dealing with multiple namespaces. |
I feel your pain, I'm also stuck with SOAP-based web services and the namespace issue is a thorn here. Going to generate XML w/ templates for now. Please do not give up :) |
In general we don't pay attention to comments that only say "+1". In fact,
I think we have a bot that deletes them.
What would be very useful instead would be pointers to programs (ideally
self-contained ones on play.golang.org) illustrating what you are trying to
do with encoding/xml, what output you wish you were getting, and how the
current output differs/breaks your use case. That information would help
guide any eventual fixes we make here.
Thanks.
|
Point taken. I do not have code right now (because I decided to generate my SOAP requests throught text/template for the moment) but thet fact is, xml.Marshal generate tags w/o any namespace. I tried to use https://github.com/gabstv/go-soap (which use xml.Marshal) and even though there are "xmlns:" declaration for many tags, none of them are in the resulting XML file. Many of the open tickets above are in the same situation. xml.Unmarshal() is fine. |
A somewhat specific example I'd like to see work properly without a lot of hacks (I normally pretend the namespace is part of the localname, which works well enough with XMPP because it's such a limited profile of XML, but won't work with more complex documents) is something like the following valid XMPP stream: <?xml version='1.0'?>
<stream:stream
from='im.example.com'
to='[email protected]'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
<stream:error>
<see-other-host xmlns="urn:ietf:params:xml:ns:xmpp-streams">im.example.net</see-other-host>
</stream:error>
</stream> which, when decoded and re-encoded will currently result in something like: <stream
xmlns="stream"
from="[email protected]"
to="im.example.com"
version="1.0"
xmlns:_xml="xml"
_xml:lang="en"
xmlns="jabber:client"
xmlns:_xmlns="xmlns"
_xmlns:stream="http://etherx.jabber.org/streams">
… Example here: https://play.golang.org/p/XyMFO301HA Note that the example reads a raw token because this is XMPP and it has to be streamed which the current XML implementation isn't very good at; that's a separate issue though. |
The issue in the above comment is effectively issue #7535 |
See #14407 . |
During the Go 1.5 development cycle, this package used to require the standard library's encoding/xml package from Go 1.5 or later, but https://go-review.googlesource.com/#/c/12772/ (which was submitted in July 2015) made an internal fork of encoding/xml, as some namespace related changes introduced in the Go 1.5 cycle were rolled back in response to golang/go#11841 Thus, this "go1.4" runtime check is no longer necessary. In the long term, this package should use the standard library's version, and the internal fork deleted, once golang/go#13400 is resolved. We could re-introduce a similar check at that time, although it could be done at compile time (via a "go1.7" build tag) instead of at runtime. Change-Id: I18258443aa3d9b519e23106aedb189f25c35495d Reviewed-on: https://go-review.googlesource.com/21634 Reviewed-by: Andrew Gerrand <[email protected]>
In particular, the Property and DeadPropsHolder types need to refer to the standard xml package, not the internal fork, to be usable by other packages. Inside the package, the XML marshaling and unmarshaling is still done by the etc/internal/xml package, and will remain that way until golang/go#13400 is resolved. Fixes golang/go#15128. Change-Id: Ie7e7927d8b30d97d10b1a4a654d774fdf3e7a1e3 Reviewed-on: https://go-review.googlesource.com/21635 Reviewed-by: Andrew Gerrand <[email protected]>
Despite poor support of XML in Golang [1], we implement de/serialization from and to XML. The resulting XML documents are correct, but verbose (no self-closing elements), and escape common characters such as signle quote (`'`). [1]: golang/go#13400
Despite poor support of XML in Golang [1], we implement de/serialization from and to XML. The resulting XML documents are correct, but verbose (no self-closing elements), and escape common characters such as signle quote (`'`). [1]: golang/go#13400
Despite poor support of XML in Golang [1], we implement de/serialization from and to XML. The resulting XML documents are correct, but verbose (no self-closing elements), and escape common characters such as signle quote (`'`). [1]: golang/go#13400
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
@ydnar Can you make a PR? |
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
Hi any plan to validate and merge the associated PR to fix namespaces soon ? |
Unfortunately we don't have a plan for how to do that without breaking existing working programs. |
Hi ! Thank you for this reply. As we don't want to break existing programs, how about introducing |
@kilianpaquier Yes, I suspect that encoding/xml/v2 is the path forward. But it's a lot of work and I'm not aware of anybody working on it. |
Hi @ianlancetaylor, what would be missing from this pull request for a first step ? It seems to reference and fix all issues linked to this issue ? |
Experience tells us that any change to encoding/xml that affects existing programs causes some of them to break. This change (https://go.dev/cl/355353) may be the exception but I don't see why. |
https://go.dev/cl/355353 might be part of a workable solution, it might not. json/v2 has had a long development process, and JSON is considerably less complex than XML. I'm hesitant to advocate for (or commit to) a similar approach, even though long-term this probably the right idea. Maintaining bug-compatibility with v1 via a wrapper seems unworkable for the same reasons encoding/xml hasn't changed. I've experimented with alternative approaches that could form the basis of an xml/v2 package, but nothing far enough along to propose anything concrete. @ianlancetaylor how would you think about confining the changes in https://go.dev/cl/355353 behind a GODEBUG flag or go.mod Go version flag? |
For
|
All issues of golang#13400 which are not new functionalities have fixes. There are minor incompatibilities between them due to the handling of prefixes. Duplicating a prefix or an namespace is invalid XML. This is now avoided. XML produced is always valid. Tests have been added for each fix and example and previous tests fixed as output is already more compact is some cases. encoding/xml: fix duplication of namespace tags by encoder (golang#7535) A tag prefix identifies the name space of the tag and not the default name space like xmlns="...". Writing the prefix is incorrect when it is bound to a name space using the standard xmlns:prefix="..." attribute. This fix skips this print. Consequences are - duplication is avoided in line with name space standard in reference. - the _xmlns declaration does not appear anymore. To keep the previous behaviour, the prefix is printed in all other cases. Token now always produces well-formed XML except when the explicit name space collides with the prefix. Made prefix handling "xmlns="space" xmlns:_xmlns="xmlns" _..." has be removed in all wants of tests. In some cases, useless declarations like xmlns:x="x" are still added in line with previous behavior. encoding/xml: fix unexpected behavior of encoder.Indent("", "") (golang#13185, golang#11431) MarshalIndent and Marshal share code. When prefix and indent are empty, the behavior is like Marshal when it should have a minimal indent, i.e. new line as in documentation. A boolean is added to the local printer struct which defaults to false. It is set to true only when MarshalIndent is used and prefix and indent are empty. encoding/xml: fix overriding by empty namespace (golang#7113) The namespace defined by xmlns="value" can be overridden in every included tag including by the empty namespace xmlns="". Empty namespace is not authorized with a prefix (xmlns:ns=""). Method to calculate indent of XML handles depth of tag and its associated namespace. The fix leaves the method active even when no indent is required. An XMLName field in a struct means that namespace must be enforced even if empty. This occurs only on an inner tag as an overwrite of any non-empty namespace of its outer tag. To obtain the xmlns="" required, an attribute is added. encoding/xml: fix panic on embedded unexported XMLName (golang#10538) By laws of reflection, unexported fields are unreachable by .Value. XMLName are allowed at any level of an inner struct but the struct may not be reachable. If XMLName field is found without name, it cannot be exported and the value is discarded like other fields. Some comments have been to underline where the issue arises. Another XMLName test was incorrectly set up and is fixed. Various cases added in a specific test. encoding/xml: fix panic of unmarshaling of anonymous structs (golang#16497) Encoding/xml is using type "typinfo" which provides its own "value" method using the reflection package. It fails to check for the documented possible panics of the reflection methods. It is impossible to fix the method as the parameter is also using reflection. The fix is to discard anonymous structs which have no value anyway. Encoder/xml documentation already mentions that fields are accessed using the reflection package. A relevant test is added. encoding/xml: fix closing tag failure (golang#20685) Push/pop of elements name must be done using the eventual prefix together with the tag name. The operation is moved before the translation of prefix into its URI. One end element of a test is fixed as expecting the last used namespace is incorrect. After closing a tag using a namespace, the valid namespace must be taken from the opening tag. encoding/xml: add check of namespaces to detect field names conflicts (golang#8535, golang#11724) Comparing namespaces of fields was missing and false conflicts were detected. encoding/xml: fix invalid empty namespace without prefix (golang#8068) Empty namespace is allowed only if it has no prefix. An error message is now returned if a prefix exists. A similar case when no prefix is provided, thus with syntax xmlns:="..." is also rejected. encoding/xml: fix normalization of attributes values (golang#20614) The attribute value was read as text. The existing attribute reader logic is fixed as an attribute may have a namespace or only a prefix. Other possibilities have been removed. To keep the behavior of raw token which allows many faults in attributes list, error handling is heavily using the Strict parameter of the decoder. Specific tests have been added including list of attributes. To keep the behavior or unmarshal, escaped characters handling has been added but it is not symmetrical to Marshal for quotes but follows XML specification. encoding/xml: fix absence of detection of another : in qualified names (golang#20396) The occurrence of second : in space and tag name is rejected. Fixes: golang#7113, golang#7535, golang#8068, golang#8535, golang#10538, golang#11431, golang#13185, golang#16497, golang#20396, golang#20614, golang#20685 Change-Id: Ib4a60347a47d23ff59b63307cebb83b71c7c9165 Commit originally authored by: Constantin Konstantinidis <[email protected]> encoding/xml: fix printing of namespace prefix in tag names Prefix displays in XML when defined in tag names. The URL of the name space is also returned for an End Token as documentation requires to improve idempotency of Marshal/Unmarshal. Translating the prefix is popping the NS which was unavailable for translation. Translate for an End Token has been moved inside the pop element part. Fixes golang#9519 Change-Id: Id7a14d07c106a76a487b5c4e28d9d563fe061c60 encoding/xml: restore changes lost in merge See golang#43168. encoding/xml: gofmt encoding/xml: minimalIndent -> minIndent to shrink diff encoding/xml: gofmt encoding/xml: revert changes to (*Decoder).attrval from master encoding/xml: restore (*printer).writeIndent to master encoding/xml: remove comments that don’t change functionality of tests encoding/xml: use t.Errorf instead of fmt.Errorf encoding/xml: fix test case for golang#11496 encoding/xml: revert unrelated change in (*Decoder).readName encoding/xml: remove comments encoding/xml: remove comments encoding/xml: edit comments encoding/xml: add test to ensure '@' is legal in an attr val encoding/xml: improve test error quoting for readability encoding/xml: (*Decoder).nsname now strictly parses namespaces encoding/xml: disable 3/4 tests for golang#43168 I’m not sure what the right fix is for this. Malformed namespaces (leading or trailing colons, more than 1 colon) should result in an error. encoding/xml: allow callers to specify namespace prefix A preferred namespace prefix can now be specified by setting the local name space to "prefix:space". In a struct tag: struct Message { XMLName struct{} `xml:"urn:ietf:foo foo:message"` Value string `xml:"urn:ietf:foo foo:value"` } encoding/xml: use tag prefixes if previously defined Instead of having redundant xmlns= attrs. encoding/xml: rename (*printer).popPrefix to popPrefixes since it pops 1 or more prefixes encoding/xml: rename printer struct fields To reflect the fact that prefixes apply to tag names, not just attrs. encoding/xml: url->uri; reorg comments encoding/xml: fix comments encoding/xml: elide redundant xmlns= attrs for default namespace (no prefix) encoding/xml: move namespace prefix tests encoding/xml: handle namespace prefixes at the root element encoding/xml: handle namespaced prefixed attr names encoding/xml: split out prefixes during reflection, not parsing encoding/xml: rename tagName to xmlTag; document it encoding/xml: separate printing xmlns:prefix= attr from creating prefix encoding/xml: clean up comments encoding/xml: don’t use xmlTag in attr serialization encoding/xml: test nested prefixed namespaced tags encoding/xml: add test for prefixed XML tags without xmlns encoding/xml: update doc comments encoding/xml: update comments encoding/xml: preemptively declare and record all namespace prefixes Immediate children of XML tags that share a namespace and prefix will now have that namespace prefix declared on the parent tag. Explicitly declared namespace prefixes via xmlns:prefix="uri" attributes will now have their values recorded and reused for marshalling child tags. This commit reorganizes how namespace prefixes are recorded and detected, along with how start tags are recorded via a slice of element structs. Some tests are changed, because the value of the marshalled XML has changed, but the resulting XML should be equivalent (and correct). encoding/xml: restore tests with xmlns:prefix= attrs to existing behavior Use xmlnsURL instead of xmlnsPrefix to specify namespace prefixes. encoding/xml: prefer unprefixed names where possible encoding/xml: add EncodeToken tests for explicit prefixes encoding/xml: add test cases for prefixed local names encoding/xml: revert change in 8f143f5277; do not preemptively declare XML namespaces encoding/xml: fix typo in test
There probably needs to be a community lead project that could work on There have been several efforts led by individuals to tackle specific problems with the current stdlib implementation. These should be combined into one project so additional improvements can be included in one module. Not to mention many commonly requested features when using XML in applications (XPath, digital signatures, XSDs, etc) |
Many legacy systems use XML only. |
workaround: |
There are many pending issues related to the handling of xml name spaces. We need to rethink this. One more try and then we're going to give up.
The text was updated successfully, but these errors were encountered: