how can you use xmllint to validate against an internally specified xsd?
Asked Answered
V

2

8

I know you can use the xmllint command to validate against local xsd files, or against xsd network file locations, but what I'd like to do is instruct xmllint to validate an XML file against its "internally specified" xsd, for instance this XML specifies an XSD location:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ord-archive:XXX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                    xsi:schemaLocation="http://somewhere/something.xsd">
 ...

Is there any way to use xmllint and specify to "validate" against its internally specified schemaLocation?

Verduzco answered 14/6, 2017 at 22:26 Comment(0)
G
1

xsi:schemaLocation should contain a list of URIs, semantically grouped two by two. Each URI appearing at an odd position specifies a namespace, and the URI appearing at the next even position specifies the location hint of the schema to use for this namespace. Location hints may be local or remote.

This is an example with three namespaces:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ord-archive:XXX
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xmlns:ord-archive="http://www.example.com/namespace" 
    xmlns:prefix="http://www.example.com/another-namespace" 
    xmlns:other-prefix="http://www.example.com/and-another-namespace" 

    xsi:schemaLocation="
      http://www.example.com/namespace
      http://somewhere.example.com/something.xsd
      http://www.example.com/another-namespace
      some-relative-path-to-a-directory/some-local-file.xsd
      http://www.example.com/and-another-namespace
      file:///some-absolute-path-to-a-directory/some-local-file.xsd">

   <!-- the document itself, which contains elements in the three
        namespaces, referenced with the bound prefixes. -->
</ord-archive:XXX>

The XML Schema specification leaves some degree of freedom to validation engines, so in theory they may or may not follow the hints, they may use cached versions for some namespaces, etc. However, in practice, most validation engines will use your hints or provide an implementation-specific way of controlling their behavior.

The example given under the link in the question uses xsi:noNamespaceSchemaLocation, which contains a location hint for a schema with no target namespace.

Grantor answered 15/6, 2017 at 7:10 Comment(0)
V
1

Appears that there are no command line options to do what I want (use the XML location hints to auto-download xsd's etc). But you might be able to manually create an XSD file and pass that on the command line to xmllint --schema xxx, possibly create an "over arching" XSD if you need multiple: https://mcmap.net/q/563144/-xmllint-validate-an-xml-file-against-two-xsd-schemas-envelope-payload

Verduzco answered 27/7, 2017 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.