Taking a course in Web Technology, I suddenly found myself editing a lot of XML files, so I hurried to fine-tune VIM to make editing more efficient. Please note that I am by no means an expert in XML, so please correct me, if I have misunderstood something. Configuration in xml.vim. Features:
- Tag closing with code snippets from xmledit. Press <C-k> once to close the tag, and twice to close it and leave an empty line between.
- Tag closing using omni-completion: <C-b>. This is used when the cursor is not at the end of the starting tag. Will insert the closing tag for the inner most unclosed starting tag.
- Cycle between start and end tags with <LocalLeader>5 (typically 5 or ,5)
- Remove outer tag with <LL>r
- Goto parent tag with <LL>u (working on it, doesn’t work with <x/> tags. Anybody got a clue how to do this correctly?)
- Document pretty printing with <LL>xf
- Wellformedness checking (<LL>xx), DTD validation (<LL>xd) and XML Schema validation (<LL>xs) with XML Catalog support using xmllint (see below)
Have a look at Vim as XML Editor by Tobias Reif to find more tips.
Validating using xmllint
Tobias Reif’s howto shows a nice way of validating your document without saving them. The approach uses xmllint from libxml2, which is very fast. It has xml catalog support, so it can translate public, system and uri identifiers to local files. Nice when you do not have to make changes in the document to validate it… Read the howto for further information, but in short, you have to:
- Specify the catalog files in .bashrc (xmllint and other validators reads (or should read) this environment variable).
export XML_CATALOG_FILES="$HOME/Docs/xml/catalog /etc/xml/catalog"
- Maintain a list of local schemas, and refer to them in the catalog files.
<?xml version="1.0"?> <!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"> <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"> <group prefer="public" xml:base="file:///home/user/Docs/xml/schemas/"> <uri name="http://www.w3.org/2001/XMLSchema" uri="xmlschema/1_0/xsd/XMLSchema.xsd" /> ... </group> </catalog> - Make xmllint know which schema to use. For DTD it is easy, as xmllint can find it from the DOCTYPE declaration in the document, and then resolve the schema from the catalog. See xml.vim for how to do that in VIM. Unfortunately, it is not that easy with XML Schemas. xmllint can’t use the URI’s from the document namespace declaration, nor can it use the reference in the schemaLocation attribute, so we cannot take advantage of catalog resolution here. If you know a validator that can do this, please let me know, as I couldn’t find one. However, I decided that I couldn’t live without this feature
, so I made a workaround, though a bit clumsy (see below).
XML Schema workaround
To add support for xml catalog resolution, we need to implement it ourselves. I have written a simple URI resolver (ResolveURI.java) using the Apache XML Resolver classes. It prints the resolved local URL to stdout if it can find an uri entry in the catalogs, or returns the argument if it can’t. To get quick access to it, we wrap it in a shell script:
#!/bin/bash DIR=/path/to/resolveruri java -cp $DIR/resolver.jar:$DIR ResolveURI $@
Normally, the Resolver classes expect the catalogs to be in a properties file, but as we use XML_CATALOG_FILES instead, we put the file CatalogManager.properties in the class path, containing just the word
catalogs=
Now we should be ready to do at least catalog URI resolving manually. Try to run the shell script with an URI like http://www.w3.org/2001/XMLSchema as argument.
The only thing left is to make VIM call the validator. We can specify the document namespace URI as the schema identifier, or use the reference from the schemaLocation attribute. In either case, it should be placed in the “s register. For namespaces, this could be done quickly by placing the cursor over the URI and typing vi”"sy. Then we call xmllint, with the resolved Schema file:
execute "%w !xmllint --noout --schema `resolveuri " . @s . "` -"
It works with multiple namespaces, as long as they are imported into the main Schema file, with the schemaLocation attribute is specified. I admit this solution is not particularly elegant, so if you know a better solution, please let me know. Although xmllint has limited support for XML Schema, at least you can now take advantage of it’s decent speed… Perhaps one should implement the URI resolver in C++, and without utilizing libraries to catch up with xmllint? Anyway, enjoy