Linux xmllint schema validation fails but error code 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux xmllint schema validation fails but error code 0
# 1  
Old 07-01-2013
shell script xmllint schema validation error code 0

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

myinput.xml:
Code:
<myinput><header>mytestvalue</header></myinput>

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

Command:
Code:
$xmllint --schema myschema.xsd myinput.xml

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

Command:
Code:
$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?

Last edited by mknag; 07-02-2013 at 09:54 AM..
# 2  
Old 07-02-2013
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.
# 3  
Old 07-02-2013
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.
# 4  
Old 07-02-2013
Actually, I get something totally different from your input:

Code:
$ 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

$

# 5  
Old 07-03-2013
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:
Code:
xmllint --schema myschema.xsd myinput.xml

Result:
Code:
$ 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:
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Xmllint parser error : EntityRef: expecting ';'

Hi I have an XML file which contains html urls in that node values. When i use xmllint to parse that, i am getting error (because of the sympols in the url). i have used --html option but it throws other tag errors. Please guide me. sample file.xml <abc> <bcd> <cde> <a>sometext</a>... (2 Replies)
Discussion started by: ananan
2 Replies

2. Shell Programming and Scripting

awk works on Linux but fails on Solaris

On linux i have the below command working fine. awk '/<app-deploy>/{A=1;++i} /<\/app-deploy>/{print >> "found"i".tmp";A=0} A{;print >> "found"i".tmp"}' deploy.xml But the same is failing on Solaris Output: awk: syntax error near line 1 awk: bailing out near line 1 uname -a SunOS mymac 5.10... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Shell Programming and Scripting

Grep works on Linux but fails on Solaris

Hi, On linux i have the below command working fine. grep -o '<name>.*</name>' deploy.tmp | sed 's/\(<name>\|<\/name>\)//g' deploy.tmp But the same is failing on Solaris uname -a SunOS mymac 5.10 Generic_150400-23 sun4v sparc sun4v Can you tell me how can i get it work on Solaris ?... (6 Replies)
Discussion started by: mohtashims
6 Replies

4. UNIX for Dummies Questions & Answers

Tcp connection to Linux server fails

I am trying to send json messages to a port on a linux server from a remote server running a .net program. I have one implementation running with successful incoming messages to port 1514. I tried to replicate the same thing but just to another port but cannot get it to work as I get the following... (3 Replies)
Discussion started by: unienewbie
3 Replies

5. UNIX for Advanced & Expert Users

Grub installation fails in Linux BMR..

Hi all, This topic is purely on a problem in installing grub in my LINUX BMR process... A major problem too.. I am designing Linux BMR where i do the following.. Backup the " / " as a whole .. with partition details ..etc And using the Knoppix Live CD i first create the partitions... (1 Reply)
Discussion started by: selvarajvs
1 Replies

6. Shell Programming and Scripting

Validation of linux scripts

Hi, I have a script which invokes another script inside. Is there any way I can validate the script for syntax errors as well as checking if the files mentioned in the commands exist without executing it? Below is an example Example. ======================= Script1 #ksh echo... (3 Replies)
Discussion started by: krish000
3 Replies

7. UNIX for Dummies Questions & Answers

read fails in Unix, but succeeds in Linux. Why?

Hi, When I use "read" to parse the sftp command sent via ptty, I ran into a very mysterious case below: --- In Unix, the command ‘quit' is lost. === Fri 13Apr12 15:42:47GMT-sftp_send_command: SENT Fri 13Apr12 15:42:47GMT-sftp_read_resp_line: Parse buffer=quit^M --> Command sent correctly... (2 Replies)
Discussion started by: HgHK
2 Replies

8. Cybersecurity

LDAP rfc2307bis.schema or nis.schema?

Hi guys, we are implementing new ldap in our organization exclusively for Linux authentication purposes. As we are new to the subject, I would appreciate help regarding which schema to use to create group of users and accounts / passwords entries? I see both schemas listed in subject above... (0 Replies)
Discussion started by: Lastminute
0 Replies

9. Shell Programming and Scripting

perl linux file name validation

Hi Everyone, #!/usr/bin/perl $a = ".a!"; if ($a =~ s///g) { print "invalid file name\n"; } else { print "valid file name\n"; } but the output is: Invalid range "_-." in regex; marked by <-- HERE in m// at ./a.pl line 5. the linux file name should be A-Z, a-z,... (8 Replies)
Discussion started by: jimmy_y
8 Replies
Login or Register to Ask a Question