Category Archives: xml

how to use xmlns prefix with xsl transformation

XSL transformation was working with an xml file without any explicit xmlns namespace, but failed on file with explicit namespace such as in below code: <FpML xmlns=”http://www.mynamespace” xmlns:xsi=””> Reason is because by default, XSL version 1.0 is considering default namespace to be blank. As such, in order to match a file with explicit namespace, possibilities are: remove namespace from input file change xsl by: adding a namespace prefix in header that is matching the input namespace <xsl:stylesheet xmlns:xsl=”” version=”1.0″> now becomes: <xsl:stylesheet xmlns:t=”http://www.mynamespace” xmlns:xsl=”” version=”1.0″> prefix all nodes used in xpath requests with previously defined prefix <xsl:param name=”data” select=”/FpML/dealEntry/eventAction” /> […]

xml indentation in linux

XML beautifier in linux can be easily done using libxml tools. install xml library sudo apt-get install libxml2-utils Run the following command xmllint –format –output <path_to_output> <input_file> That’s it! Apparently this tool can be used for smooth integration within gedit but this made gedit crash on my computer. sources

xpath query – filtering by node content is not working [solved]

Let’s say you have the following xml: <?xml version=”1.0″ ?> <user> <firstname>MyName</firstname> </user> If you want to select the firstname node containing the value “MyName”, do NOT proceed as follow: $xpath = ‘/user/firstname = “MyName”‘; But rather do: $xpath = ‘/user/firstname[. = “MyName”]’; Similarly, let’s say you want to return <user> node instead of <firstname> node, now your xpath query is simply: $xpath = ‘/user[firstname = “MyName”]’; Note as we wrote [firstname = “xxx”] and NOT [/firstname = “xxx”]. In this case, when we add the forward slash, we test root node <firstname> and not the node within our current […]

relaxng validation: specify range limit for integer

If you want to specify range limit for integers, you may be interested in the following parameters: minInclusive maxInclusive minExclusive maxExclusive Note that ‘min’ and ‘max’ parameters do NOT exist, you must specify whether boundaries are included or not. Example <define name=”mytype.content”>  <data type=”positiveInteger”>   <param name=”minInclusive”>1</param>   <param name=”maxInclusive”>31</param>  </data> </define> With above example, validation will fail on any input that’s not an integer or that’s not within [1:31] range. sources

XML validation & query urls (& failure)

I tried to make my website xml-valid so that it can be loaded as xml by browsers that accept it (currently firefox3, chrome & opera) and be parsed faster. Moreover, I simply wanted my website to be w3c valid with an XHTML header. Problems come when you have urls with arguments in your page. Validation fails upon ‘&’ variable separator. At first, what I did was simply to htmlencode the separator, replacing ‘&’ with ‘&amp;’. It works fine regarding content validation, but breaks down server-side code as ‘&amp;’ is not treated as the separator by php if raw url is […]

Validate content with regular expression within relax ng

If you want to use regular expression within your relax-ng schema to validate content, you can do as follow: <element name=”group_ids”>  <data type=”string”>   <param name=”pattern”>(d+,?)*</param>  </data></element> sources

match any node with relaxng

To match any node with relax ng, you need to define your pattern step by step. Ie. first you need to specify that any node name is to be matched (using special node <anyName/>) and then define your typical content. For example, if you want to match any node name, but still restrict these nodes to string content only, you can do the following: <element> <anyName/> <data type=”string”/> </element> Read official tutorial (section 11) for more details sources official tutorial (section 11)

Subclass / Overload relax-ng definitions

Relax-ng is so great, you can subclass definitions to better match your needs. Let’s say you’ve make a definition of an element in file elem1.rng as follow: <define name=”elem1.class”>..def1..</define> Now you want to modify this element . Add extra choice Let’s say you want elem1 to be able to match either def1 or another definition called def2. To add a choice node, simply redefine this element in your current relax-ng file adding the ‘combine=”choice”‘ attribute, as follow: <define name=”elem1.class” combine=”choice”>..def2..</define> Now it’s the same as having: <define name=”elem1.class”><choice><..def1..><..def2..></choice></define> Add extra requested content Let’s say now that you want to add […]

Validate MySQL datetime format with relax-ng

Key for validating string content with relax-ng is to user the “pattern” parameter. A MySQL datetime format is as follow: 2009-01-13 13:25:06 # output of ‘select NOW();’ Corresponding relax-ng validation schema is: <!– define mysql datetime format content –> <define name=”global.mysql_datetime”> <data type=”string”><param name=”pattern”>[0-9]{4}(-[0-9]{2}){2} [0-2][0-9](:[0-5][0-9]){2}</param></data> </define> Now you can simply call it in your relax-ng document using <ref name=”global.mysql_datetime”>, for example to ensure <creation_date> node has mysql datetime formated content, you could use: <element name=”creation_date”><ref name=”global.mysql_datetime”/></element> sources

XMLReader fails to load relax-ng schema

I don’t know why php XMLReader library fails to load all my xml-schemas including some pretty basics. Each time I end up with the following message: Warning: XMLReader::setRelaxNGSchemaSource() [xmlreader.setrelaxngschemasource]: Unable to set schema. This must be set prior to reading or schema contains errors. in /var/www/test.php on line 12 source code Here is the source code that generated above error: <?php $rng_schema = <<<RNG <?xml version=”1.0″?> <grammar xmlns=”″ datatypeLibrary=””> <start> <element name=”node1″><text/></element> </start> </grammar> RNG; $xml_reader = new XMLReader(); $xml_reader->setRelaxNGSchemaSource($rng_schema); workaround My relax-ng schemas looks like working if using xmllint (part of libxml2-tools package) or sun-microsystem “msv” validator ( Both […]