xmllint : validate an XML file against two XSD schemas (envelope / payload)

前端 未结 3 971
甜味超标
甜味超标 2021-02-02 13:04

I am using xmllint to do some validations and I have an XML instance document which needs to validate against two schemas: one for the outer \"envelope\" (which includes an

相关标签:
3条回答
  • 2021-02-02 13:29

    If you had an import element in your A.xsd, right after opening the schema tag,

    <xsd:import namespace="http://www.example.org/B" schemaLocation="B.xsd"/>
    

    then you could pass A.xsd to xmllint and it would work with:

    xmllint -schema A.xsd ab.xml
    
    0 讨论(0)
  • 2021-02-02 13:32

    You can create a wrapper schema and import both namespaces. AB.xsd:

    <?xml version="1.0" encoding="UTF-8"?>
    <schema elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema">
        <import namespace="http://www.example.org/A" schemaLocation="A.xsd"/>
        <import namespace="http://www.example.org/B" schemaLocation="B.xsd"/>
    </schema>
    

    Then:

    xmllint --schema AB.xsd ab.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <a:someType xmlns:a="http://www.example.org/A" xmlns:b="http://www.example.org/B" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/A A.xsd                             http://www.example.org/B B.xsd">
    
                <b:person>
                    <b:firstName>Mary</b:firstName>
                    <b:lastName>Bones</b:lastName>
                </b:person>
    
    </a:someType>
    ab.xml validates
    
    0 讨论(0)
  • 2021-02-02 13:46

    I quit on xmllint and used Xerces instead.

    I downloaded Xerces tarball and after exploding it to some local folder I created the following validate script based on this suggestion (from web archive - original link being now dead):

    #!/bin/bash
    XERCES_HOME=~/software-downloads/xerces-2_11_0/
    echo $XERCES_HOME
    java -classpath $XERCES_HOME/xercesImpl.jar:$XERCES_HOME/xml-apis.jar:$XERCES_HOME/xercesSamples.jar sax.Counter $*
    

    The ab.xml file is then validated, against both schemas, with the following command:

     validate -v -n -np -s -f ab.xml
    

    Xerces is reading the schema locations from the xsi:schemaLocation element in ab.xml so they don't need to be provided in the command line invocation.

    0 讨论(0)
提交回复
热议问题