xsd

package module
v0.0.0-...-03ca754 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2019 License: BSD-3-Clause Imports: 5 Imported by: 3

README

XSD - XML Schema Validator

XSD is a Go wrapper for the C libxml2 library.

Quick Start

See example_test.go for how to use this library to validate a document.

Documentation

http://godoc.org/github.com/krolaw/xsd

Special Thanks

Special thanks to Michal Pristas and Travis Cline for independently providing solutions for returning validation errors.

WOW!

An employee of HP Inc requested I attach a licence so it could be used in their "FitStation" platform - Awesome.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DocPtr

type DocPtr C.xmlDocPtr

type Schema

type Schema struct {
	Ptr C.xmlSchemaPtr
}

func ParseSchema

func ParseSchema(buffer []byte) (*Schema, error)

ParseSchema creates new Schema from []byte containing xml schema data. Will probably change []byte to DocPtr.

func (*Schema) Validate

func (s *Schema) Validate(doc DocPtr) SchemaErrors

Validate uses its Schema to check an xml doc. If the doc fails to match the schema, a list of errors is returned, nil otherwise.

Example
package main

import (
	"github.com/jbussdieker/golibxml"
	"github.com/krolaw/xsd"

	"fmt"
	"unsafe"
)

const (
	XSD = `<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="DayCount">
		<xs:simpleType>
		<xs:restriction base="xs:int">
			<xs:minInclusive value="0" />
			<xs:maxInclusive value="9999" />
		</xs:restriction>
		</xs:simpleType>
	</xs:element>
</xs:schema>`

	XML = `<DayCount>-1</DayCount>`
)

func main() {

	xsdSchema, err := xsd.ParseSchema([]byte(XSD))
	if err != nil {
		fmt.Println(err)
		return
	}

	doc := golibxml.ParseDoc(XML)
	if doc == nil {
		// TODO capture and display error - help please
		fmt.Println("Error parsing document")
		return
	}
	defer doc.Free()

	// golibxml._Ctype_xmlDocPtr can't be cast to xsd.DocPtr, even though they are both
	// essentially _Ctype_xmlDocPtr.  Using unsafe gets around this.
	if err := xsdSchema.Validate(xsd.DocPtr(unsafe.Pointer(doc.Ptr))); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("XML Valid as per XSD")
}
Output:


Element 'DayCount': [facet 'minInclusive'] The value '-1' is less than the minimum value allowed ('0').
Element 'DayCount': '-1' is not a valid value of the local atomic type.
Example (Invalid)
package main

import (
	"github.com/jbussdieker/golibxml"
	"github.com/krolaw/xsd"

	"fmt"
	"unsafe"
)

const XSD = `<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="DayCount">
		<xs:simpleType>
		<xs:restriction base="xs:int">
			<xs:minInclusive value="0" />
			<xs:maxInclusive value="9999" />
		</xs:restriction>
		</xs:simpleType>
	</xs:element>
</xs:schema>`

func main() {

	xsdSchema, err := xsd.ParseSchema([]byte(XSD))
	if err != nil {
		fmt.Println(err)
		return
	}

	doc := golibxml.ParseDoc("<BadTag>3</BadTag>")
	if doc == nil {
		// TODO capture and display error - help please
		fmt.Println("Error parsing document")
		return
	}
	defer doc.Free()

	// golibxml._Ctype_xmlDocPtr can't be cast to xsd.DocPtr, even though they are both
	// essentially _Ctype_xmlDocPtr.  Using unsafe gets around this.
	if err := xsdSchema.Validate(xsd.DocPtr(unsafe.Pointer(doc.Ptr))); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("XML Valid as per XSD")
}
Output:

Element 'BadTag': No matching global declaration available for the validation root.

type SchemaErrors

type SchemaErrors []string

func (SchemaErrors) Error

func (e SchemaErrors) Error() string

Jump to

Keyboard shortcuts

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