~samwhited/clarkns

clarkns/ns.go -rw-r--r-- 1.1 KiB
7eacd4eaSam Whited Add boilerplate documentation and CI config 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2019 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// Package clarkns implements Clark Notation for XML.
//
// For more information see http://www.jclark.com/xml/xmlns.htm
package clarkns // import "mellium.im/clarkns"

import (
	"encoding/xml"
	"errors"
	"strings"
)

// Errors returned by methods and functions in this package.
var (
	ErrUnclosedNS = errors.New("clarkns: no namespace closing bracket found")
)

// Name parses the provided XML name from Clark notation.
// For example the string "{http://www.cars.com/xml}part" would result in a
// local name of "part" and a namespace of "http://www.cars.com/xml"
//
// Name does not enforce XML name constraints and there is no guarantee that the
// returned xml.Name is valid.
func Name(s string) (xml.Name, error) {
	var name xml.Name
	if s == "" {
		return name, nil
	}

	// Parse out the namespace.
	if s[0] == '{' {
		s = s[1:]
		idx := strings.IndexByte(s, '}')
		if idx == -1 {
			return name, ErrUnclosedNS
		}
		name.Space = s[:idx]
		s = s[idx+1:]
	}

	name.Local = s
	return name, nil
}