Linux xmllint schema validation fails but error code 0

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?

The writer saved $? for more extreme errors.

Make a small valid file and see what is says for perfect. Capture stderr 2>&1 or stdout $( ... ) and compare for that string.

Thanks for your reply.
So, the only option is to "grep | awk" the stdout for "validates/ fails" that is written in the console (or redirected to a tmp file) after xmllint?
But, just wondering when we use a schema to validate an xml, the ideal expectation would be a failure if the xml is not compliant to the schema. Anyways, there is always an alternative.

Actually, I get something totally different from your input:

$ cat my*

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


$ xmllint --schema myschema.xsd myinput.xml

<?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

$ echo $?
3

$

Interesting!! What is your xmllint version?
Mine is "xmllint: using libxml version 20510"

Here is the output that I received by running the below command:

xmllint --schema myschema.xsd myinput.xml

Result:

$ xmllint --noout --schema myschema.xsd myinput.xml
Element myinput: child header should not be present
myinput.xml fails to validate

$ echo $?
0

My "AWK"ward Solution:

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