Validate the XML content using regexp

Hi,

XML is as below.

  <?xml version="1.0" encoding="Shift_JIS"?>
  <Filling_data>
    <destination_ID>05</destination_ID>
    <sender_ID>01</sender_ID>
    <type>PGFILE  </type>
    <remaining_capacity>00000</remaining_capacity>
 </Filling_data>

Here i want to validate each value in tag, am trying to validate XML input based on regexp. My validate function is failing to validate the xml tag. Even if i enter <destination_ID>09</destination_ID> or <destination_ID>05</destination_ID>, it always prints not Destination_ID, can you please let me what is the issue.

package main
import (
	"encoding/xml"
	"fmt"
	"golang.org/x/text/encoding/japanese"
	"golang.org/x/text/transform"
	"gopkg.in/validator.v2"
	"io"
	"os"
)
type PGS_filing_data struct {
	Destination_ID     string `xml:"destination_ID" 	        Motu:"regexp=(01|05)"`
	Sender_ID          string `xml:"sender_ID" 			sender_ID:"regexp=(01|05)"`
	Type_ID            string `xml:"type" 				type_ID:"regexp=(PGFILE△△|PGFILER△|PGALL△△△)"`
	Remaining_capacity string `xml:"remaining_capacity" remaining_capacity:"regexp=([0-9]{3}[0-5][0-9]|△△△△△)"`
}
// DoSampleImpl ...
func DoSampleImpl() {
	// Read XML File. Note! This file is SJIS encoding.
	fp, err := os.Open(cs)
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer fp.Close()
	fp.Seek(0, 0)
	// bind xml string
	decoder := xml.NewDecoder(fp)
	decoder.CharsetReader = func(charset string, r io.Reader) (io.Reader, error) {
		return transform.NewReader(r, japanese.ShiftJIS.NewDecoder()), nil
	}
	data := new(PGS_filing_data)
	err = decoder.Decode(&data)
	if err != nil {
		fmt.Println(err.Error())
		rt.Information = err.Error()
	}
	fmt.Println("[!!!!", data, "]")
	// show result
	fmt.Println("[", data.Destination_ID, "]")
	t1 := validator.NewValidator()
	t1.SetTag("Motu")
	if err := t1.Validate(data.Destination_ID); err != nil {
		fmt.Println("!", data.Destination_ID, " is not Destination_ID")
	} else {
		fmt.Println(" -", data.Destination_ID, "is valid Destination_ID")
	}
	return
}

I managed to get this running and found the problem. It is in:

if err := t1.Validate(data.Destination_ID); err != nil {
	fmt.Println("!", data.Destination_ID, " is not Destination_ID")
} else {
	fmt.Println(" -", data.Destination_ID, "is valid Destination_ID")
}

The error returned by t1.Validate when printed (which this code does not do) displays unsupported type. It is common practice to include the actual error in whatever error handling you do so that this sort of message is not missed.

The validator package expects a struct type as input so it can use the struct tags to determine how to validate things. If you change t1.Validate(data.Destination_ID) to t1.Validate(data), the validation will succeed without an error.

If you change t1.Validate(data.Destination_ID) to t1.Validate(data), the validation will succeed without an error.
I tried the same, and passed data. t1.Validate(data), and change the input XML to <destination_ID>09</destination_ID>, it still says 09 is valid Destination. and when I change the input XML to <destination_ID>05</destination_ID>, then it rightly says valid Destination.

According to Motu:"regexp=(01|05), it should give validation is true only for 01 and 05, but here what ever is the <destination_ID> value, set in XML it always says valid Destination. Can you please give me pointers to resolve this.

Removing the tab character (\t) from:

`xml:"destination_ID" 	        Motu:"regexp=(01|05)"`

causes validation to work as expected. I tested it by switching destination_ID in the xml between 05 and 06.

Thank you, it worked.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.