Skip to content
mai 22 / David Regnier

Script de validation XML avec vérification XSD

Comment valider vos fichiers XML liés à un XSD sous Linux

Prérequis:

  • Cygwin x64

Détail du processus:
- Pour chaque fichier dans le répertoire XML
- Parse et valide le fichier
- Le programme produit un fichier error.log si une erreur est détectée
- Vous pouvez également ajouter un paramètre « no_xsd » pour uniquement vérifier si vos fichiers sont « well-formed »

############################################################################################
# Helper for XML validation (ERS V3) or Well-formed
#
# How to run the program:
#	Just drag into Cygwin this script, add param [no_xsd] (to test only well-formed XML)
#
# Author:
#	David REGNIER
# Arguments:
#	no_xsd (if we want to check only well-formed), no argument is needed for XSD validation
# Returns:
#   0 No error
#	3 Error
#############################################################################################

# Init variables
NOW=$( date +"%Y-%m-%d %H:%M:%S" ) # Set current date
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) # Set current working directory
INPUT_XML_FOLDER="$DIR/XML"
INPUT_XSD_ROOT_FILE="$DIR/XSD/V3/ers-operations-3.xsd"
LOG_FOLDER="$DIR/Log"
FILES_NB=$(ls -1 $INPUT_XML_FOLDER/ | wc -l) # Count files by folder

# Starting
echo "$NOW Starting program..."
i=0

rm -f "$LOG_FOLDER/stats.log" # Delete log file if exist
rm -f "$LOG_FOLDER/error.log" # Delete log file if exist

# Testing folder
if find $INPUT_XML_FOLDER -maxdepth 0 -empty | read v; then
	echo -n "$NOW ERROR, no XML files found in $INPUT_XML_FOLDER, please advise!"
	exit 3
else
	# Begin process
	echo "$NOW Found $FILES_NB XML file(s) in folder"    

	if [[ $1 == "no_xsd" ]]; then
		echo $(date +"%Y-%m-%d %H:%M:%S") "Processing Well-formed process"
		for FILES in `ls $INPUT_XML_FOLDER`
		do
			xmllint --noout $INPUT_XML_FOLDER/$FILES 2>> "$LOG_FOLDER/stats.log"
			let i++
			echo -n $(date +"%Y-%m-%d %H:%M:%S") "Processing file: [$FILES] $i/$FILES_NB               " $'\r'
		done
	else
		echo $(date +"%Y-%m-%d %H:%M:%S") "Processing Validates with XSD process"
		for FILES in `ls $INPUT_XML_FOLDER`
		do
			xmllint --noout --schema $INPUT_XSD_ROOT_FILE $INPUT_XML_FOLDER/$FILES 2>> "$LOG_FOLDER/stats.log"
			let i++
			echo -n $(date +"%Y-%m-%d %H:%M:%S") "Processing file: [$FILES] $i/$FILES_NB               " $'\r'
		done
	fi;	

	echo '' # Clear console line

	# Delete validates line and redirect to error.log
	cat "$LOG_FOLDER/stats.log" | sed '/validates/d' >> "$LOG_FOLDER/error.log"

	rm -f "$LOG_FOLDER/stats.log" # Delete log file if exist

	# Check if error, advise user
	if [[ -s "$LOG_FOLDER/error.log" ]]; then
		echo -n $(date +"%Y-%m-%d %H:%M:%S") "ERROR, failed to validate XML, please check the log file $LOG_FOLDER/error.log"
	else
		rm -f "$LOG_FOLDER/error.log" # Delete log file if exist
		echo -n $(date +"%Y-%m-%d %H:%M:%S") "SUCCESS, all file(s) has been validated"
		exit 0
	fi;
fi
Voici le fichier de démonstration en exemple:
helpers_validate_xml_by_xsd.zip
Laisser un commentaire


3 + = 9