element.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Copied and modified from Go 1.14 stdlib's encoding/xml
  5. package xml
  6. // A Name represents an XML name (Local) annotated
  7. // with a name space identifier (Space).
  8. // In tokens returned by Decoder.Token, the Space identifier
  9. // is given as a canonical URL, not the short prefix used
  10. // in the document being parsed.
  11. type Name struct {
  12. Space, Local string
  13. }
  14. // An Attr represents an attribute in an XML element (Name=Value).
  15. type Attr struct {
  16. Name Name
  17. Value string
  18. }
  19. /*
  20. NewAttribute returns a pointer to an attribute.
  21. It takes in a local name aka attribute name, and value
  22. representing the attribute value.
  23. */
  24. func NewAttribute(local, value string) Attr {
  25. return Attr{
  26. Name: Name{
  27. Local: local,
  28. },
  29. Value: value,
  30. }
  31. }
  32. /*
  33. NewNamespaceAttribute returns a pointer to an attribute.
  34. It takes in a local name aka attribute name, and value
  35. representing the attribute value.
  36. NewNamespaceAttribute appends `xmlns:` in front of namespace
  37. prefix.
  38. For creating a name space attribute representing
  39. `xmlns:prefix="http://example.com`, the breakdown would be:
  40. local = "prefix"
  41. value = "http://example.com"
  42. */
  43. func NewNamespaceAttribute(local, value string) Attr {
  44. attr := NewAttribute(local, value)
  45. // default name space identifier
  46. attr.Name.Space = "xmlns"
  47. return attr
  48. }
  49. // A StartElement represents an XML start element.
  50. type StartElement struct {
  51. Name Name
  52. Attr []Attr
  53. }
  54. // Copy creates a new copy of StartElement.
  55. func (e StartElement) Copy() StartElement {
  56. attrs := make([]Attr, len(e.Attr))
  57. copy(attrs, e.Attr)
  58. e.Attr = attrs
  59. return e
  60. }
  61. // End returns the corresponding XML end element.
  62. func (e StartElement) End() EndElement {
  63. return EndElement{e.Name}
  64. }
  65. // returns true if start element local name is empty
  66. func (e StartElement) isZero() bool {
  67. return len(e.Name.Local) == 0
  68. }
  69. // An EndElement represents an XML end element.
  70. type EndElement struct {
  71. Name Name
  72. }
  73. // returns true if end element local name is empty
  74. func (e EndElement) isZero() bool {
  75. return len(e.Name.Local) == 0
  76. }