Help us shape the future of Go! Take the 2026 Go Developer Survey

xmltest

package module
v0.0.0-...-1abcdaa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2015 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Package xmltest provides utilities for XML testing.

I contributed this code to golang.org/x/net/webdav but found it might be
useful outside that package. Use it to compare if XML content is equal on
a semantic but not on byte level.

Note: The normalised XML output of this package is not equivalent to
Canonical XML.  If there is enough interest I might get into that.

This package requires a recent Go 1.5 release candidate.

Documentation

Overview

Package xmltest provides utilities for XML testing.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Normalizer

type Normalizer struct {
	// OmitWhitespace instructs to ignore whitespace between element tags.
	OmitWhitespace bool
	// OmitComments instructs to ignore XML comments.
	OmitComments bool
}

Normalizer normalizes XML.

func (*Normalizer) EqualXML

func (n *Normalizer) EqualXML(a, b io.Reader) (bool, error)

EqualXML tests for equality of the normalized XML contents of a and b.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/rsto/xmltest"
)

func main() {
	var n xmltest.Normalizer
	s1 := `<root xmlns="space"/>`
	s2 := `<s:root xmlns:s="space"></s:root>`
	fmt.Println(n.EqualXML(strings.NewReader(s1), strings.NewReader(s2)))
}
Output:
true

func (*Normalizer) Normalize

func (n *Normalizer) Normalize(w io.Writer, r io.Reader) error

Normalize writes the normalized XML content of r to w. It applies the following rules

  • Rename namespace prefixes according to an internal heuristic.
  • Remove unnecessary namespace declarations.
  • Sort attributes in XML start elements in lexical order of their fully qualified name.
  • Remove XML directives and processing instructions.
  • Remove CDATA between XML tags that only contains whitespace, if instructed to do so.
  • Remove comments, if instructed to do so.

Note that the normalized XML content might differ from canonicalized XML as defined by W3C.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL