lapots
lapots

Reputation: 13395

how to check whether xml file is valid

I'm learning xml. I create small xml files and dfd files. This is xml file

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="stylesheet.css"?>
<!DOCTYPE software SYSTEM "software.dtd"
[
<!ATTLIST title ttl CDATA "no name">
<!ATTLIST license lcs CDATA #REQUIRED>
]>
<software>
<application>
    <suite>
        <app>
            <title ttl="MOffice">Microsoft Office</title>
            <developer>Microsoft</developer>
            <os>Microsoft Windows</os>
            <license>Trialware</license>
        </app>
        <app>
            <title>iWork</title>
            <developer>Apple</developer>
            <os>Mac OS</os>
            <license>Proprietary</license>
        </app>
        <app>
            <title ttl="Office">Open Office</title>
            <developer>StarOffice,OpenOffice.org,Apache OpenOffice</developer>
            <os>Linux, Mac OS, Microsoft Windows</os>
            <license>LGPL,Apache License</license>
        </app>
    </suite>
</application>  
</software>

And this is dfd file

<!ELEMENT software(application)>
<!ELEMENT application(suite) >
<!ELEMENT suite(app*)>
<!ELEMENT app(title,developer,os,license)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT developer(#PCDATA)>
<!ELEMENT os(#PCDATA)>
<!ELEMENT license(#PCDATA)>
<!ATTLIST os osx CDATA #REQUIRED>

It should be incorred xml file because there is no attributes osx and lcs in os and license elements. But Internet Explorer opens it normal. Why?

Upvotes: 1

Views: 517

Answers (2)

questionto42
questionto42

Reputation: 9512

Python example for the answer above - the ... are placeholders, an xsd file must be one full abstract structure of what has to be in the xml:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema ...>
   <xs:complexType name="xyz">
...
   </xs:complexType>
   <xs:complexType name="abc">
...
   </xs:complexType>
...

Just get an xsd file from wherever, there must be one to validate against.

Python file: "validate.py"

import xmlschema

my_xml = '/path/to/file.xml'
some_xsd_validation_schema = '/path/to/file.xsd'

xmlschema.validate(my_xml, some_xsd_validation_schema)

Then run python validate

Upvotes: 0

Oded
Oded

Reputation: 498914

Most XML parsers, including the one in Internet Explorer, only check XML for well-formedness - to ensure that the XML structure is correct.

In order to validate against a DTD or XML Schema, you need to use a validating parser.

The exact process of how to use such a parser for validation depends on the parser and programming language.

Upvotes: 2

Related Questions