Skip to content

Instantly share code, notes, and snippets.

@jtojnar
Last active December 18, 2015 22:08
Show Gist options
  • Save jtojnar/5851893 to your computer and use it in GitHub Desktop.
Save jtojnar/5851893 to your computer and use it in GitHub Desktop.

This is no longer maintained. Please check my fork.

I wanted SublimeLinter to automatically validate XML against XSD, but this AFAIK isn’t possible so I made a wrapper for xmllint utility to find XSD file in source code and pass it as argument to xmllint. Because XSD paths can be relative I had to change xml.py to use INPUT_METHOD_FILE (@ instead od -) so I could detect file path. Currently the wrapper is quite stupid but it can be changed when I come across something that doesn’t work.

Tested with SublimeLinter3, libxml version 20902 and Python 3.4.3

Save xmllintschema to ~/bin directory (or another directory on path) and set executable bit (chmod +x xmllintschema).

  1. Click PreferencesBrowse Packages → SublimeLinter-xmllint, open linter.py

  2. Replace xmllint --noout * - with xmllintschema --noout * @

Known problems

  • Only works properly when lint mode is set to Save Only due to INPUT_METHOD_FILE.
  • If you get following error in console, try setting absolute path in linter.py.

    SublimeLinter: ERROR: xmllint cannot locate 'xmllintschema'

#!/usr/bin/env python
import os
import sys
import re
import subprocess
schema = None
schemaline = -1
i = 0
with open(sys.argv[-1], 'r') as f:
for line in f:
i += 1
match = re.match(r'.* xsi:noNamespaceSchemaLocation="([^"]+)".*', line)
if match:
schema = match.group(1)
schemaline = i
if schema is not None:
online = schema.find('http://') == 0 or schema.find('https://') == 0
if not (online or os.path.isabs(schema)):
schema = os.path.dirname(sys.argv[-1]) + '/' + schema
if not online and not os.path.isfile(schema):
print('-:' + str(schemaline) + ': Schema file does not exist', file=sys.stderr)
sys.exit()
args = ['xmllint', '--schema', schema]
args.extend(sys.argv[1:])
else:
args = ['xmllint']
args.extend(sys.argv[1:])
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
result = process.communicate()[0].decode('utf-8')
if result.find('Failed to locate the main schema resource at') > -1:
print('-:' + str(schemaline) + ': Schema file does not exist', file=sys.stderr)
sys.exit()
if result.find('WXS schema ' + schema + ' failed to compile') > -1:
print('-:' + str(schemaline) + ': Error in parsing schema', file=sys.stderr)
sys.exit()
print(result, file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment