xml-validation
REST service for the validation of xml
against a XML Schema (xsd
).
install & activate
Create a file .git/hooks/pre-push
in the GIT repository with the following
content:
#!/bin/bash
# In the case of "git push --tags", writes the short tag value to the
# VERSION file in the top level directory of the Git repository
toplevel=$(git rev-parse --show-toplevel)
f=${toplevel}/VERSION
if [ ! -f "$f" ]; then
touch "$f"
git add "$f" 2>/dev/null
fi
old_version=$(cat $f)
new_version=$(git describe --tags --abbrev=0)
if [ "$old_version" != "$new_version" ]; then
echo "$new_version" > "$f"
git commit -am "update version to '"$new_version"'"
git push origin # Is required because normal push does not necessarily happen
fi
exit 0
Then make this file executable:
chmod 755 .git/hooks/pre-push
Do the following steps with root permission on the server installation:
mkdir -p /usr/local/share/xml-validation
cd /usr/local/share/xml-validation
curl --silent --output - \
https://gitlab1.ptb.de/niepra01/xml-validation/-/archive/master/xml-validation-master.tar \
| tar xf - --strip-components=1 # only once
python3 -m venv .
source bin/activate
pip3 install pip-tools
pip-compile
chown -R nobody.nobody ../xml-validation
systemctl daemon-reload # if already exist
systemctl link $PWD/xml-validation.service
systemctl enable xml-validation.service # make permanent
systemctl start xml-validation.service
systemctl status xml-validation.service # running?
journalctl -f --unit xml-validation.service # run-time check
Configuration
File config.json
:
{
"server":{
"host":"0.0.0.0",
"port": 5005
},
"xsd":{
"filename":"dcc.xsd",
"externalBaseURL":"https://ptb.de/dcc/"
}
}
-
externalBaseURL
: Must be ended with a path seperator
API
/validate endpoint [POST]
curl -X POST -H "Content-Type: text/xml" -d @valid_xml_matches_xsd http://localhost:5005/validate
<ok/>
Alternative call with wget
:
wget --post-file=example.xml --header="Content-Type:text/xml;" -q -O- http://localhost:5005/validate
curl -X POST -H "Content-Type: text/xml?v=2.1.0" -d @no_valid_xml http://localhost:5005/validate
<error>unvalid xml data</error>
curl -X POST -H "Content-Type: text/xml" -d @valid_xml_but_dont_matches_xsd http://localhost:5005/validate
<error>verbose error message</error>
Example:
curl -X POST -H "Content-Type: text/xml" -d @11044_17_DCC_v1.8.1_Au_with_error.xml http://localhost:5005/validate
<error>failed decoding 'A' with XsdAtomicBuiltin(name='xs:double'):
Reason: could not convert string to float: 'A'
Schema:
<simpleType xmlns="http://www.w3.org/2001/XMLSchema" xmlns:hfp="http://www.w3.org/2001/XMLSchema-hasFacetAndProperty" id="double" name="double">
<annotation>
<appinfo>
<hfp:hasFacet name="pattern" />
<hfp:hasFacet name="enumeration" />
<hfp:hasFacet name="whiteSpace" />
<hfp:hasFacet name="maxInclusive" />
<hfp:hasFacet name="maxExclusive" />
<hfp:hasFacet name="minInclusive" />
<hfp:hasFacet name="minExclusive" />
<hfp:hasProperty name="ordered" value="total" />
<hfp:hasProperty name="bounded" value="true" />
<hfp:hasProperty name="cardinality" value="finite" />
<hfp:hasProperty name="numeric" value="true" />
</appinfo>
<documentation source="http://www.w3.org/TR/xmlschema-2/#double" />
</annotation>
<restriction base="xs:anySimpleType">
<whiteSpace fixed="true" id="double.whiteSpace" value="collapse" />
</restriction>
</simpleType>
Instance:
<ns0:coverageProbability xmlns:ns0="https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/SI">A</ns0:coverageProbability>
Path: /{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}digitalCalibrationCertificate/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}measurementResults/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}influenceConditions/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}influenceCondition/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}outcome/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}data/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/DCC}quantity/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/SI}list/{https://intranet.ptb.de/fileadmin/dokumente/intranet/abteilungen/abteilung_1/Digitaler_Kalibrierschein/SI}coverageProbability
</error>
/version [GET]
Returns the version of the validation server:
curl http://localhost:5005/version
<version>0.2.0</version>
Web interface
http://localhost:5005/validation.html
A working PTB internal Service: http://a73434.berlin.ptb.de:5005/validation.html