diff --git a/main.go b/main.go index bd5dbf1..0edfa41 100644 --- a/main.go +++ b/main.go @@ -85,7 +85,7 @@ size: file.Size(), } logger := logrus.WithFields(logrus.Fields{ - "corpusName": fileData.corpusName, + "corpusname": fileData.corpusName, "name": fileData.name, "startAt": fileData.startAt, "extension": fileData.extension, diff --git a/xml.go b/xml.go index ba0307a..d9a02cb 100644 --- a/xml.go +++ b/xml.go @@ -193,13 +193,35 @@ tmpXpath := "{" if err == nil { for _, field := range configDetailledAnalyze.XML.XPATH { + previousXpath := make(map[string]string) for _, xpathContent := range field.Xpath { goodXPath, hasQuote := FormatXpathByType(field.Type, xpathContent) expr, _ := xpath.Compile(goodXPath) result := expr.Evaluate(xmlquery.CreateXPathNavigator(doc)) - str := fmt.Sprintf("%v", result) - if len(str) > 0 && str != "0" { - tmpXpath += FormatTextForXpath(field, str, hasQuote) + text := fmt.Sprintf("%v", result) + // not process xpath if previous is good + if previousXpath[xpathContent] == "" && len(text) > 0 { + previousXpath[xpathContent] = xpathContent + // if regex add substring isValid + if field.Regex != "" { + filterText := regexp.MustCompile(field.Regex).FindString(text) + if len(filterText) > 0 && filterText != "0" { + tmpXpath += `"` + field.Name + `IsValid":` + "true" + `,` + } else { + tmpXpath += `"` + field.Name + `IsValid":` + "false" + `,` + } + } + // if fieldType = Count authorize 0 value + if field.Type == "Count" { + tmpXpath += `"` + field.Name + `":` + text + `,` + } else if text != "0" { + // for string + if hasQuote { + tmpXpath += `"` + field.Name + `":"` + strings.NewReplacer("\n", "").Replace(text) + `",` + } else { // for boolean and number + tmpXpath += `"` + field.Name + `":` + text + `,` + } + } } } } @@ -220,20 +242,6 @@ logger.Info("") } -func FormatTextForXpath(field MetadataConfigDetailledAnalyze, text string, hasQuote bool) string { - if field.Regex != "" { - filterText := regexp.MustCompile(field.Regex).FindString(text) - if len(filterText) > 0 { - text = filterText - } - } - - if hasQuote { - return `"` + field.Name + `":"` + strings.NewReplacer("\n", "").Replace(text) + `",` - } - return `"` + field.Name + `":` + text + `,` -} - func FormatXpathByType(fieldType string, xpath string) (string, bool) { switch fieldType { case "Number":