<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mntnoe.com &#187; validation</title>
	<atom:link href="http://mntnoe.com/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://mntnoe.com</link>
	<description></description>
	<lastBuildDate>Mon, 12 Sep 2011 07:19:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>XML editing and validation in VIM</title>
		<link>http://mntnoe.com/2008/02/xml-editing-and-validation-in-vim/</link>
		<comments>http://mntnoe.com/2008/02/xml-editing-and-validation-in-vim/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 23:29:01 +0000</pubDate>
		<dc:creator>Mads Navntoft Noe</dc:creator>
				<category><![CDATA[Editors]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml catalog]]></category>

		<guid isPermaLink="false">http://www.mntnoe.com/?p=26</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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  <a title="xml.vim" href="http://www.mntnoe.com/wp-content/uploads/2008/02/xml.vim">xml.vim</a>. Features:</p>
<ul>
<li>Tag closing with code snippets from <a href="http://www.vim.org/scripts/script.php?script_id=301">xmledit</a>. Press <em>&lt;C-k&gt;</em> once to close the tag, and twice to close it and leave an empty line between.</li>
<li>Tag closing using omni-completion: <em>&lt;C-b&gt;</em>. 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.</li>
<li>Cycle between start and end tags with <em>&lt;LocalLeader&gt;5</em> (typically <em>5</em> or <em>,5</em>)</li>
<li>Remove outer tag with <em>&lt;LL&gt;r</em></li>
<li>Goto parent tag with <em>&lt;LL&gt;u</em> (working on it, doesn&#8217;t work with  &lt;x/&gt; tags. Anybody got a clue how to do this correctly?)</li>
<li>Document pretty printing with <em>&lt;LL&gt;xf</em></li>
<li>Wellformedness checking (<em>&lt;LL&gt;xx</em>), DTD validation (<em>&lt;LL&gt;xd</em>) and XML Schema validation (<em>&lt;LL&gt;xs</em>) with XML Catalog support using xmllint (see below)</li>
</ul>
<p>Have a look at <a href="http://www.pinkjuice.com/howto/vimxml/index.xml">Vim as XML Editor</a> by Tobias Reif to find more tips.<span id="more-26"></span></p>
<h3>Validating using xmllint</h3>
<p>Tobias Reif&#8217;s howto shows a nice way of validating your document without saving them. The approach uses <a href="http://xmlsoft.org/xmllint.html">xmllint</a> from libxml2, which is very fast. It has xml catalog support, so it can translate <em>public</em>, <em>system</em> and <em>uri</em> identifiers to local files. Nice when you do not have to make changes in the document to validate it&#8230; Read the howto for further information, but in short, you have to:</p>
<ol>
<li>Specify the catalog files in .bashrc (xmllint and other validators reads (or should read) this environment variable).
<pre> export XML_CATALOG_FILES="$HOME/Docs/xml/catalog /etc/xml/catalog"</pre>
</li>
<li>Maintain a list of local schemas, and refer to them in the catalog files.
<pre>&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
"http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"&gt;
&lt;catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog"&gt;
  &lt;group prefer="public" xml:base="file:///home/user/Docs/xml/schemas/"&gt;
    &lt;uri name="http://www.w3.org/2001/XMLSchema"
      uri="xmlschema/1_0/xsd/XMLSchema.xsd" /&gt;
  ...
  &lt;/group&gt;
&lt;/catalog&gt;</pre>
</li>
<li>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 <a title="xml.vim" href="http://www.mntnoe.com/wp-content/uploads/2008/02/xml.vim">xml.vim</a> for how to do that in VIM. Unfortunately, it is not that easy with XML Schemas. xmllint can&#8217;t use the URI&#8217;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&#8217;t find one. However, I decided that I couldn&#8217;t live without this feature <img src='http://mntnoe.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  , so I made a workaround, though a bit clumsy (see below).</li>
</ol>
<h3>XML Schema workaround</h3>
<p>To add support for xml catalog resolution, we need to implement it ourselves. I have written a simple URI resolver (<a title="ResolveURI" href="http://www.mntnoe.com/wp-content/uploads/2008/02/resolveuri.java">ResolveURI.java</a>) using the <a href="http://xml.apache.org/commons/components/resolver/">Apache XML Resolver</a> classes. It prints the resolved local URL to stdout if it can find an <em>uri</em> entry in the catalogs, or returns the argument if it can&#8217;t. To get quick access to it, we wrap it in a shell script:</p>
<pre>#!/bin/bash
DIR=/path/to/resolveruri
java -cp $DIR/resolver.jar:$DIR ResolveURI $@</pre>
<p>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</p>
<pre>catalogs=</pre>
<p>Now we should be ready to do at least catalog URI resolving manually. Try to run the shell script with an URI like <em>http://www.w3.org/2001/XMLSchema</em> as argument.</p>
<p>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 <em>&#8220;s</em> register. For namespaces, this could be done quickly by placing the cursor over the URI and typing <em>vi&#8221;"sy</em>. Then we call xmllint, with the resolved Schema file:</p>
<pre>execute "%w !xmllint --noout --schema `resolveuri " . @s . "` -"</pre>
<p>It works with multiple namespaces, as long as they are <em>import</em>ed 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&#8217;s decent speed&#8230; Perhaps one should implement the URI resolver in C++, and without utilizing libraries to catch up with xmllint? Anyway, enjoy <img src='http://mntnoe.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p align="center"><a title="vim-xml.tar.bz2" rel="attachment wp-att-29" href="http://www.mntnoe.com/2008/02/xml-editing-and-validation-in-vim/vim-xmltarbz2/">Download tar.bz2 </a></p>
]]></content:encoded>
			<wfw:commentRss>http://mntnoe.com/2008/02/xml-editing-and-validation-in-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

