title: 'iPhone: libxml2 & RELAX NG validation' author: Marcus Rohrmoser type: post date: 2010-05-28T11:18:51+00:00 url: /2010/05/iphone-libxml2-relax-ng-validation/ yourls_shorturl:
Having a validating parser in place can reduce the required code to parse XML a lot – you know very well what you actually get. As mentioned in my last post about RELAX NG & trang, I prefer RELAX NG over W3C XML Schema – which doesn't matter anyway because Apple's suggested XML parser doesn't validate at all.
So we have to go one level deeper and have a look at libxml2.
The libxml2 examples didn't help me too much either, but luckily there's xmllint available in source (OSS just rocks) which does almost what we want. It first parses the XML into a
xmlDocPtr and validates afterwards – and it does so for a reason:
You can have a validating
<a href="http://xmlsoft.org/html/libxml-xmlreader.html#xmlTextReaderRelaxNGSetSchema">xmlTextReaderRelaxNGSetSchema</a>), but it won't detect IDREFs missing their referred to ID and the error messages lack the name of the failing item. BTW – when validating against a W3C schema this ID/IDREF check isn't available yet.
I finally discarded streaming XML parsing in favour of validation and „push“ parsing (nice for data coming in over the wire) and did:
relaxngschemas) – similar to xmllint schema loading,
xmlCreatePushParserCtxt) exactly like xmllint,
xmlTextReaderif you want a SAXish programming model.
I may prepare and publish a
<a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html">NSXMLParser</a> and firing it's callbacks in order to easily switch validating and non-validating parser implementations, but this has to wait a bit. Stay tuned.