linux xmllint schema validation fails but error code 0
Asked Answered
U

2

6

Command line xmllint --schema validation fails but $? returns 0

myinput.xml:

<myinput><header>mytestvalue</header></myinput>

myschema.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="myinput" type="xsd:string"/>
</xsd:schema>

Command:

$xmllint --schema myschema.xsd myinput.xml

Result:

Element myinput: child header should not be present
myinput.xml fails to validate

Command:

$echo $?

Result:

0

Could someone tell me why xmllint schema validation failure is not returned as an error? Or suggest me ways to capture this as an error in my shell script? In my shell script, current I am validating the above xmllint command in an "if" block and it fails only for xml well-formedness but succeeds for schema validation failure.

if the above is not returned as error, should I go about doing the ugly way of "grep fails" on the xmllint output to figure-out if schema validation succeeded or failed? Any thoughts?

Uno answered 1/7, 2013 at 1:41 Comment(2)
could someone provide some inputs on this?Uno
Are you reading $? imediately after the xmllint line or a few lines of code later? $? pertains to the return code of the preceding statement, if you've got an echo in between or something you'll be reading that echo's $?.Lordosis
U
0

There doesn't seem to be any better way other than the "AWK"ward way that I mentioned in my question. Anyways, here is how, I am working around:

$xmllint --noout --schema myschema.xsd myinput.xml >> $tmpFile
schemaResult=$(cat $tmpFile | grep myinput.xml | awk '{ print $2 }')
if [ "x$schemaResult" = "xvalidates" ];
    echo "Schema validation succeeded"
else
    echo "Schema validation failed"
fi
Uno answered 2/7, 2013 at 20:57 Comment(0)
L
0

I wonder what version of xml utils you are using.

arturcz@szczaw:/tmp/stack$ xmllint --schema myschema.xsd myinput.xml; echo $?
<?xml version="1.0"?>
<myinput><header>mytestvalue</header></myinput>
myinput.xml:1: element myinput: Schemas validity error : Element 'myinput': Element content is not allowed, because the type definition is simple.
myinput.xml fails to validate
3

The 3 is result of echo $?. It works for me for following versions:

  • libxml2-utils 2.9.1+dfsg1-3 (on Debian unstable) xmllint: using libxml version 20901
  • libxml2-utils 2.7.8.dfsg-2+squeeze8 (on Debian squeeze) xmllint: using libxml version 20708

Please forgive me if it's obvious to you, but you need to remember that you shall not run any command between xmllint call and echo $?. So, try the exact call I put above - both commands in one line separated with semicolon. In that way you can verify if it's really problem with the xmllint.

If above sugestion won't help you, please provide output of xmllint --version and the shell you are using.

Larrikin answered 13/11, 2013 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.