milesma
milesma

Reputation: 1591

How to validate xml with a XSD import/include other XSD files?

The root one:

a.xsd

Which imports | includes:

<xsd:import schemaLocation="b.xsd"/>
<xsd:include schemaLocation="c.xsd"/>

I know there are lots of online tools (validator) can have one XML file and a single one .xsd file as input and run the validation.

Considering the "import" and "include" are involved, what are the options to validate an XML file by just specify a single .xsd file?

Which GUI tool (a free one) can be used to do a quick validation? How to implement in code of any one of the languages Java/C#/C++/Python?

Thank you in advance

Upvotes: 4

Views: 2215

Answers (1)

milesma
milesma

Reputation: 1591

  1. The answer is simple: put following files in the same folder.

    • a.xsd
    • b.xsd
    • c.xsd
  2. I've wrote a Validator.py with following contents:

    import sys
    from lxml import etree
    doc = etree.parse(sys.argv[1])
    xmlschema_doc = etree.parse('a.xsd')
    xmlschema = etree.XMLSchema(xmlschema_doc)
    if xmlschema(doc):
        print 'Success!'
    else:
        print 'Invalid!'
        xmlschema.assertValid(doc)
    raw_input()
    
  3. Execute in command line (Windows):

    python Validator.py aParser.xml
    

Upvotes: 1

Related Questions