-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxsdvalidate
More file actions
executable file
·57 lines (47 loc) · 1.37 KB
/
xsdvalidate
File metadata and controls
executable file
·57 lines (47 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh
xml_file=$1
shift
# Show usage if no arguments are provided
if [ -z "$xml_file" ]; then
echo "Validate an XML file against its schema"
echo ""
echo "Usage: $0 <xml_file> [additional options to pass to xmllint]"
echo "If xml_file is -, read from standard input"
exit 1
fi
# Check if xmllint is available
if ! command -v xmllint &> /dev/null
then
echo "xmllint could not be found"
exit 1
fi
tmp_file=""
# If xml_file is -, read the file to check from standard input to a temporary file
if [ "$xml_file" = "-" ]; then
tmp_file=$(mktemp)
if [ -z "$tmp_file" ]; then
echo "Could not create temporary file"
exit 1
fi
cat > "$tmp_file"
xml_file="$tmp_file"
fi
# Get the namespace of the root element
root_namespace=$(xmllint $xml_file --nonet --noout --xpath "namespace-uri(/child::*)")
if [ -z "$root_namespace" ]; then
echo "Could not determine the namespace of the root element"
if [ -n "$tmp_file" ]; then
rm "$tmp_file"
fi
exit 1
fi
echo "Validating against $root_namespace"
# Validating requires the schema namespace to be present in the system XML catalog
# Show command being run
echo "xmllint $xml_file --nonet --noout --schema $root_namespace ${@}"
xmllint "$xml_file" --nonet --noout --schema "$root_namespace" "${@}" 2>&1
# Clean up
if [ -n "$tmp_file" ]; then
rm "$tmp_file"
fi
exit 0