1

I am new to shell scripting.

Requirement:

I want to check if the XML is well formed or not. I don't have a schema or something to validate it against. I just want to check that it's well formed.

Ex of correct XML for me:

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 

Incorrect XML for me:

<heading>Reminder</head>
<body>Don't forget me this weekend!</>

What I have tried so far:

I wrote a shell script command

xmllint --valid filename.xml  // replaced the filename.xml with my file.

Error I am getting:

valid_xml.xml:2: validity error : Validation failed: no DTD found !

The XML which I used for which I am getting error:

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from> 
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
RobC
  • 22,977
  • 20
  • 73
  • 80
Cpt Kitkat
  • 1,392
  • 4
  • 31
  • 50
  • 2
    The term you're actually looking for is _["well formed"](https://stackoverflow.com/questions/134494/is-there-any-difference-between-valid-xml-and-well-formed-xml)_ (not valid) – RobC Dec 02 '20 at 16:59

1 Answers1

3

When you use --valid xmllint will attempt to validate the DTD, since you do not have one, it will fail with the error message (Validation failed: no DTD found !).

To check whether the xml document is "well formed" use the following

if xmllint filename.xml > /dev/null ; then
    echo "Valid"
else
    echo "Fail"
fi

Note that none of the sample files provided by OP (except the last example) are well formed. The sample file marked correct is missing the top level XML tag. It should be:

<root>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body> 
</root>
RobC
  • 22,977
  • 20
  • 73
  • 80
dash-o
  • 13,723
  • 1
  • 10
  • 37