For the complete documentation index, see llms.txt. This page is also available as Markdown.

Validating XML Using XSD

XML Schema Definition Language — or XSD — is a powerful tool for declaring a data model and asserting a dataset conforms to it. While it is complex and carries XML's verbosity tax, XSD has few equally comprehensive and flexible peers in modeling data.

CsvPath Framework offers the usual two approaches to validating data using XSDs:

  • Standalone validation resulting in a live metadata

  • Embedded in the Framework's comprehensive data preboarding workflow

The former is great for pipelines, API backends, scripts. The latter is the option to choose if you have strict governance requirements and need an immutable archive and source of truth at the ingress point.

Validating XML is very similar to validating using any of CsvPath Framework's validation languages. XSD always needs a runner wherever you use it. A trivial csvpath statement is all that's needed in CsvPath Framework.

This is probably the simplest possible example:

    def validate_model_year(self):
        path = CsvPath()
        path.parse(f"""
                $imports/2026.xml[0][ xsd("xsds/automobiles.xsd") ] 
        """).fast_forward()
        assert path.is_valid is True

Happily it is also not far from being the most complex standalone XSD validation example. And as usual, you can get the errors, printouts, metadata, and variables from the CsvPath instance. If you run the collect method, instead of fast_forward, you get the matching lines as well.

A data preboarding XSD example

The full data preboarding workflow adds immutability, telemetry, no-code integrations, and historic data and metadata. To get all that, alll we need to do is register our data and load the csvpath statement driver as a named-paths group.

Here's the driver csvpath:

We're going to add this XSD to the named-paths group. That allows us to refer to it by name, not by path.

Note that we're setting a scan instruction of [0], meaning scan just the first line. When you use the xsd() function in your csvpath, the CsvPaths instances stops your path seeing any further lines, so that [0] could be [5] or [*] and the effect would be the same. The reason for this is that you are looking at the XML document as a whole. Read line by line through the file would be problematic and wasteful. If you wanted to apply XPath rules outside the XSD to the same file, you would simply create another driver csvpath using the xpath() function and add it to the same named-paths group.

The XSD could be:

The XML might be like:

With those inputs, the Python required looks like this:

You can use DataFileReader or simply use the basic Python open(), whichever works best for your use case. And, although we used the name "autos" in several places you can use whatever names you like, they don't have to all be the same.

That's all there is to it. When you run this example you'll see another run in the archive under "autos" with all your data and metadata.

Last updated