4

I have an XML file that must conform to a particular XML Schema:

<?xml version="1.0" encoding="UTF-8"?>
<myroot
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="/home/nico/schemas/myroot.xsd">
    <blabla />
</myroot>

When I edit this XML file in Vim, I would like to be told whether the syntax is OK, for instance by showing <blabla /> in red if it is not allowed by the XML Schema.

Most IDEs do that by default. Is it achievable with Vim?

If needed, I am OK with doing some particular configuration for this XML/XML Schema, as I edit this particular XML file very often. It is about a megabyte.

  • Vim isn't good at semantic highlighting -- instead, it's purely based on regular expressions. But a plugin like Syntastic could show you errors when you save the file, provided you have the necessary external programs to validate XML. – tommcdo Apr 8 '15 at 14:24
  • @tommcdo: Looks nice! If anyone know a Syntastic-compatible XML Schema-based XML validator (not just an XML validator), then it would make for a fantastic answer :-) – Nicolas Raoul Apr 9 '15 at 2:27
3

xmllint supports local schema validation, so with Syntastic + xmllint you can accomplish what you want.

Once installing both tools, add the following autocmd to your vimrc, tweaking paths to match where the files actually live:

autocmd BufNewFile,BufRead /home/nico/xml/commonly_edited.xml call s:xml_validate()
function! s:xml_validate()
    let g:syntastic_xml_xmllint_args = "--schema /home/nico/schemas/test.xsd"
endfun

This will cause xmllint to obey the schema when validating, and tag errors in the signs column on save, as seen here:

xml schema validation example

Alternatively, there are alternate syntax validators such as Xerces, which can supposedly validate based on the schemas defined inside the file directly, instead of having to manually specify. One could possibly write a Syntastic checker that uses Xerces to validate XML, but that could be a bit more involved if it doesn't spit errors out in the right format.

2

The Syntastic plugin currently contains support for xmllint from xmlsoft. If you know of another xml lint checker that verifies schema or dtd then it will be your easiest bet to create a Syntastic plugin for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.