Category Archives: xml

SimpleXML and default namespace

It looks like php 5.2.6 has a problem with xpath queries on SimpleXMLElement with a default namespace! Let’s say your xml file is the following: <?xml version=”1.0″?> <request name=”/do/action” xmlns=”mynamespace”> </request> Let’s say you create a SimpleXMLElement: $sxe = new SimpleXMLElement($xml_string); Now you want to extract content of ‘name’ parameter in ‘request’ node: $sxe->xpath(‘/request[@name]); This will not return any result because you did not registered your default namespace. Well let’s do it! First argument is the prefix, second is the namespace: $sxe->registerXPathNamespace(”, ‘mynamespace’); If you run again your xpath query, you won’t get any result! Too bad! Looks like a […]

PHP and Relax-NG validation

To validate an xml file with a Relax-NG schema in PHP, you can use the XMLReader library: $xml_reader = new XMLReader(); $xml_reader->setRelaxNGSchema($relax_ng_file); $xml_reader->XML($xml_input->asXML()); if (!$xml_reader->isValid()) throw new Exception(‘failed RelaxNG validation); I did not find how to retrieve corresponding error. If you need a more verbose relax-ng validator, you can use ‘xmllint‘ in linux command console (part of ‘libxml2’ package). sources PHP documentation on XMLReader library

xml namespaces

XML namespaces are very easy to use, once we understand their purpose. objective Purpose of xml namespaces is to prevent namespace collision, ie. to prevent two objects having the same name but meaning two different things. Bellow is an illustration taken from Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:xmlns=”namespaceURI” This XML carries HTML table information: <table xmlns=””> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> This XML carries information about a piece of furniture: <table xmlns=””> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> how to define namespace Basically […]

Relax-NG: grouping node

Be carreful: <optional> <element name=”node1″><text/></element> <element name=”node2″><text/></element> </optional> is not the same as <optional> <element name=”node1″><text/></element> </optional> <optional> <element name=”node2″><text/></element> </optional> First code portion means that <node1> and <node2> elements must either be both present or none (ie. we cannot have one without having the other) while second code portion means that <node1> is optional and <node2> is optional too independently one from another, ie. we can have one but not the other.

Relax-NG validation fails when defining maxlength for string

With Relax-NG, you can define a maximum length for strings, ex: <element name=”identifier”> <data type=”string”> <param name=”maxLength”>30</param> </data> </element> Be careful, attribute name is maxLength and not maxlength (do not foget to capitalize letter “L”).

xmllint: Relax-NG parser error : xmlRelaxNGParse: xxx.rng is empty

I tried to validate my xml file against relax-ng file, using xmllint, and each time I got this error: Relax-NG parser error : xmlRelaxNGParse: <my_file>.rng is empty I’ve spent two hours searching the web without finding any answer. I’ve tried with relax-ng examples taken from official tutorial and still got the same error. Then I looked at relax-ng own .rng file (the one validating the validation-language itself! and looking at the source, I noticed that they defined an xmlns attribute on <grammar> node. That’s the source of the problem! Simply add this xmlns attribute to your grammar node too, […]

XML Validation: XML Schema (xsd) vs. Relax NG (rng)

At first, I learned XML Schema but found it hard to deal with unordered xml nodes, plus it’s not that easy to read. I found out Relax NG, read the tutorial and well, it does the job pretty well! its syntax is self-explanatory it allows to do anything you can do with XML Schema, with greater flexibility (cf. you can load external file, define and extend matching-classes…) it’s far easier to read and memorize you can transparently deal with unordered xml nodes (cf. <interleave> node) It looks like we can validate xml docs with both XML Schema and Relax-NG using […]