Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Schema ¶
type Schema struct {
Ptr C.xmlSchemaPtr
}
func ParseSchema ¶
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
Click to show internal directories.
Click to hide internal directories.