Relax NG Verification in .net (and a bit of Schematron)

I’ve been working with Docbook V5.0 a bit and started working on some processing tools to support my workflow. One of the big things is that the official Docbook Schema is Relax NG and Schematron.

Relax NG

In .net, you can create a validating XML Reader by passing in XmlReaderSettings into XmlReader.Create, but the built-in ValidationType is limited to W3C XML Schema (.xsd) or Document type definitions (.dtd). Docbook has Schema files for both, but neither are the official standard because of a slight lack of features in those schema languages.

Thankfully, the Mono team has made a Relax NG library and created a NuGet Package that is useable in Microsoft’s .net. The Package ID is RelaxNG:

PM> Install-Package RelaxNG

I’ve created a simple Docbook XML File for testing purposes and I’m using the docbookxi.rng schema file (since I’m using XIncludes).

// using System.Xml;
// using System.Xml.Linq;
// using Commons.Xml.Relaxng;
using (XmlReader instance = new XmlTextReader("DocbookTest.xml"))
using (XmlReader grammar = new XmlTextReader("docbookxi.rng"))
using (var reader = new RelaxngValidatingReader(instance, grammar))
    XDocument doc = XDocument.Load(reader);
    Console.WriteLine("Document is using Docbook Version " +

There are two ways of handling Validation Errors (in the test case, I’ve duplicated the <title>First Part of Book 1</title> node, which is illegal in Docbook since title can only occur once in that scenario).

If no handler is set up, this throws a Commons.Xml.Relaxng.RelaxngException with an error like Invalid start tag closing found. LocalName = title, NS = line 35, column 14.

The better way is to hook up to the InvalidNodeFound Event which has a signature of bool InvalidNodeFound(XmlReader source, string message):

reader.InvalidNodeFound += (source, message) =>
                               Console.WriteLine("Error: " + message);
                               return true;

source is the RelaxngValidatingReader as an XmlReader and allows you to look at the current state to do further analysis/error recovery. message is a human readable message like "Invalid start tag found. LocalName = title, NS =". The return value decides whether of not processing continues. If true, it will skip over the error – in the end, I’m going to have a proper XDocument but of course all guarantees for validity are off. If false, this will throw same RelaxngException as if there’s no event handler wired up.

Generally, I prefer to make use of a lambda closure to log all errors during Validation and set a bool on failure that prevents further processing afterwards.


Now, Relax NG is only one of the two parts of Docbook Validation, although arguably the bigger one. Schematron is employed for further validation, for example that book must have a version attribute if (and only if) it’s the root element, or that otherterm on a glosssee must point to a valid glossentry. The Docbook Schematron file is in the sch directory and for this test, I’ve removed the <glossentry xml:id="sgml"> node from the DocbookTest.xml file. This still passes Relax NG, but is no longer a valid Docbook document.

There isn’t much in terms of Schematron support in .net, but I’ve found a rather ancient library called Schematron.NET of which I downloaded Version 0.6 from 2004-11-02. This is messy, because I have to use the Docbook W3C XML Schema file which has embedded Schematron rules – basically docbook.xsd, xml.xsd and xlink.xsd from the /xsd directory. Thanks to this article on MSDN for pointing me to the library and to the fact that Schematron rules can be embedded into .xsd using the appinfo element.

I also need to make sure to use the XmlTextReader and not any other XmlReaderLiskov be damned!

using (XmlReader instance = new XmlTextReader("DocbookTest.xml"))
    var schemas = new XmlSchemaCollection();
    schemas.Add("", "xml.xsd");
    schemas.Add("", "xlink.xsd");
    schemas.Add("", "docbook.xsd");

    var schematron = new Validator();

This throws a NMatrix.Schematron.ValidationException with the message

Results from XML Schema validation:
  Error: Reference to undeclared ID is 'sgml'.
  At: (Line: 85, Column: 35)

There doesn’t seem to be an Event Handler, but the code is very 2004-ish, with properties being set after processing. Overall, the whole approach is very messy, I’m even validating the whole document again against XSD after it’s been passed through Relax NG already.

The library is also expecting the old Schematron 1.5 namespace of – which is fine for Docbook 5.0 but will be a problem once Docbook 5.1 comes out since it uses the ISO Schematron namespace of

For 5.0 it does give proper Schematron validation which is good enough for now, but overall, this isn’t really a great way to do Schematron validation. Not sure if there’s a better solution because I’d love to avoid starting my own .net 4.5 Schematron Validator Project 🙂

My new Lenovo Thinkpad E440

Important Update: In late 2014, Lenovo stared shipping their systems with adware that poses significant security threats to the users. I therefore recommend not buying any Lenovo products. The review will stay up for historical purposes, but my next Laptop won’t be a Lenovo.

It’s been a while since I bought a new Laptop. The last one I blogged about was an ASUS eeePC 1000HE, which is still in use as my sole Windows XP machine for interfacing with my Commodore 64 and to test games on an old Atom and GMA 950 graphics. In 2010, I bought a 13″ MacBook Pro with a 2.4 GHz Core 2 Duo which served me well until late 2013 when I wanted something with a higher screen resolution (1280×800 just wasn’t that great for some things) and a more power without sacrificing on battery life, Windows 7 compatibility or the ability to actually do work.

I ended up with a Lenovo ThinkPad E440.

Specs and Delivery

I ordered my E440 on February 24 as a BTO (Build-to-order) for a bit less than $700 including Tax and Shipping. For that money, I got

  • Intel Core i5-4200M CPU (2.5 GHz Dual Core with Hyperthreading)
  • 14.0″ 1600×900 AntiGlare Screen (16:9 Aspect Ratio)
  • Bigger Battery (62WH compared to the stock 48WH)
  • Intel 7260AC Dual Band (2.4/5 GHz) 802.11 ac/a/b/g/n Wireless
  • Windows 7 Professional 64-Bit
  • Intel HD 4600 Graphics
  • 4 GB RAM
  • 500 GB 7200 rpm hard drive and a DVD-R drive

The laptop was delivered on April 1 – that’s 36 days between ordering and delivering. This is rather ridiculous for a business laptop. Lenovo explained they had an unexpected surge of orders that clogged up their manufacturing capacity, but still, that was a bit much.

The Laptop has two memory slots, one which was filled with the 4 GB Memory I ordered and one that was empty. I had a fitting 4 GB Memory still lying around (Kingston KVR16LS11/4), so I upgraded it to 8 GB RAM immediately. I also had a 256 GB Samsung 840 Pro SSD lying around which immediately replaced the 500 GB Hard Drive.

Every Laptop should use SSDs – it makes a massive difference even over 7200 rpm drives, and the lack of a moving part increases the overall resilience. It’s also extremely quiet since pretty much only the CPU draws any real power. The Graphics is an Intel HD 4600 – not the most amazing gaming chip, but it runs Reaper of Souls in 1600×900 perfectly fine, so it’s good enough for my mobile gaming needs.

For reference, the AmazonBasics 14″ Sleeve fits perfectly, although no space for any accessories.

Anti-Theft systems and other security features

The E440 comes with Intel Anti-Theft and Computrace. Now, by virtue of being an Anti-Theft system, the Laptop will continuously send data about its location over the internet and this is enabled by default – not everyone needs it or is comfortable with it. For some in-detail look into Computrace, read this article.

Lenovo allows you to not only Enable/Disable the features, but you can even permanently disable it. They warn you that you can never re-enable it, so I assume its wiping the option ROM. After permanently disabling both Intel AT and Computrace I didn’t see any of the services that Securelist identified running.

The E440 also comes with a TPM Chip, useful for Bitlocker. A Fingerprint reader is an option as well, although I ordered mine without. Both features can be disabled in the BIOS if there’s no need for them.

Finally, UEFI Secure Boot can be toggled, but what’s even more important, you can enter “Setup Mode” which allows you to enter your own keys. This is important if you use non-Windows OS but still want to use Secure Boot.


I can’t really say too much about the preinstalled software. I noticed that it came with stuff already installed on it, but the first thing that I did was reinstall Windows on the new SSD.

Lenovo offers Windows 7 Professional as a BTO Option, which is great since there’s no good successor to it on the market yet but they include neither installation media nor a product key sticker (UPDATE: There is a real Windows 7 Product Key sticker – it sits under the battery). I had a Win7 Professional DVD lying around from another computer and used the free MagicalJellyBean Key Finder to extract the product key from the installation.

When it comes to non-Apple laptops, you should ALWAYS install them from scratch if you want a clean Windows installation without any crap on it, but as said, I haven’t done a thorough investigation on the E440 before I wiped it.

The Screen

Two features sold me immediately: The screen is Anti-Glare, and it’s 1600×900 on 14″. Anti-Glare used to be the default for laptop screens because it makes it better to work with, but with the influx of Entertainment-focused laptops in the late 90’s, the Anti-Glare was omitted, leading to screens that have deeper blacks for games/movies but make it a nightmare to work with.

From what I can see, the screen is a TN Screen, not an IPS. This means color distortion from an angle. The E440 doesn’t distort much when viewing from the side, but doesn’t have a really wide vertical angle. I’m a programmer, so that works perfectly fine for me, but if you’re in need of accurate color representation, don’t get a TN screen.

1600×900 on 14″ is awesome for me. I have enough real estate to have all the stuff open that I want and I can still read it without having to use the utterly broken Windows DPI Scaling feature. Here’s a screenshot, click for full size:

Mouse-replacement – Touchpad and Trackpoint

When it comes to Laptops, there is one major issue: The Touchpad that’s used in lieu of a mouse. Apple’s Macbook touchpad is phenomenal, it’s lightyears ahead of anything the Wintel crowd sells. The problem is that I don’t really like their current lineup of Laptops, and since I don’t use Mac OS X anyway (I run Windows 7 on both my main machine – a Mid 2010 Mac Pro – and on my MacBook Pro before I sold it) I could safely look at all the options on the market.

One of the key reasons to go Lenovo ThinkPad was because of their TrackPoint, a little “joystick” sitting in the middle of the Keyboard (between G, H and B keys) that can be used to move the mouse pointer. It takes a little bit to get used to, but then it’s pretty awesome and precise. The main touchpad is acceptable as well, but the Synaptics TouchPad driver isn’t as good as what Apple offers in Bootcamp. Specifically, scrolling with two fingers has a slight delay before it returns to normal operation and doesn’t work in all apps (e.g., in Steam it doesn’t really emulate a scroll wheel).

The Keyboard

The Keyboard itself is awesome, the chiclet style and size of the keys makes typing straight forward and easy, I didn’t have any issues hitting the right key (and only that key, not some neighboring keys as well). What has really sold me is the fact that there are dedicated PageUp/Down and Home/End/Insert/Delete keys. That actually took me a whole to get right, because after working on a MacBook Pro for a long time, I’m so used to Fn+Up for PageUp or Fn+Left for Home that I needed to retrain myself for this keyboard, but now it’s awesome for programming. The one thing I wasn’t willing to relearn though is the positioning of the Fn and Ctrl keys – Ctrl is the bottom-left key, Fn is to the right of it. Lenovo has acknowledged this and offers a BIOS option to swap Ctrl/Fn to their correct order.

The F-Keys default to their alternative mode, where F1 is “Mute” and F5 is “Brightness Down”. But again, there’s a simple way to change that, Fn+Escape switches it around so that F1 is F1 and Fn+F1 is “Mute”. This setting persists across restarts, which is awesome!

It’s definitely one of the best Laptop Keyboards I worked with.


Lenovo did a spectacular job on the BIOS. It’s a bit sad that this has to be explicitly pointed out, but they allow you to toggle or tweak almost every feature the Laptop offers. I assume that by virtue of being a business laptop, they assume that they are selling to IT System Administrators.

The Laptop isn’t too heavy and has a good battery lifetime with the 62WH battery – I can go through a whole day of working without any issue. I do not know if the 7200 rpm hard drive would draw a noticeable amount of power as I immediately replaced it with an SSD. Despite being made out of plastic, it doesn’t feel cheap, although of course it’s not in the same league as Apple’s unibody.

The touchpad cannot hold a candle to Apple. But when it comes to PC touchpads, it is definitely workable. The Trackpoint stick is a great way to control the mouse pointer as well if you’re used to it. Tap to click works pretty well, although there’s no “tap bottom right corner” to right click (you can set it to “tap with 2 fingers to right click”).

Overall, I’m very pleased with the E440 once it finally showed up on my doorstep. For the money I got almost all the specs that I wanted without having to make compromises, although of course I’m not factoring in the $200 SSD that I still had lying around. But even for the $1000 that it would cost, it’s worth the price for me.

Putting in the SSD and RAM was easy – unscrew three screws at the bottom (with a normal screwdriver, not some special nonsense), unclip the plate the screws are holding, voila, HDD and RAM is right there. This is how PC Laptops are done since forever, and this is something I missed on my MacBook Pro where changing the Hard Drive was a loathsome operation. Also, the battery is a removable part, as it should be.

I like it. A lot.