XML into PHP array using XSLT

Don’t want to read everything? Jump to the code or leave a comment

9 months ago I wrote a few lines PHP-code to get XML data into an array. The reason why I wrote this is hard to recall but in the process I had to solve a problem: how do I get a serie of values from an XML file into a PHP array?

Lots of solutions exist like the package PEAR::XML_Serializer but they were all too advanced for what I needed.
All I needed was a simple function or class that would do just that: convert xml file with values to php array. And I found a simple trick using nothing more than XSLT.

If you look at an array in PHP, the data inside is a storable presentation of a value. To get the value back into a string you have to call the serialize() function. Getting the array back from the string, you guessed it, call the unserialize() function. I’m not getting any deeper into this, the manual entries are very clear and the array section of the PHP manual is very good.

Once you know how a serialized array looks like, you can generate it with whatever you have. You can generate it through an XSLT transformation, like I did.

Look at this PHP array:

$arr = array(
	"path" => " /var/www/admin/",
	"url"=> "http://www.domain.be/",
	"webmaster" => "someEmail@address.be",
	"lang" => "en-uk"
);
echo serialize($arr);

This produces the following result:

a:4:{
	s:4:"path";
	s:21:" /var/www/admin/";
	s:3:"url";
	s:19:"http://www.domain.be/";
	s:9:"webmaster";
	s:17:"someEmail@address.be";
	s:4:"lang";
	s:5:"en-uk";
	}

First it tells you how many key-value pairs are stored: 4 in this case.
It iterates through all the members and counts the number of characters for each key and each value, finally adding the actual value.

The original XML I wanted to convert was a config file with a few entries. The file looks like this:

< ?xml version="1.0"?>
<configuration>
<entry name="path"_
value="/var/www/admin/"/>
<entry name="url"_
value="http://www.domain.be/"/>
<entry name="webmaster_
value="someEmail@address.be"/>
<entry name="lang" value="EN"/>
</configuration>

A simple configuration file for an online application.
Using XSLT, you can easily transform this XMl tree into a string that matches the serialized string I mentioned above.

The XSLT file looks like this:

< ?xml version="1.0"?>
<xsl :stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/_
1999/XSL/Transform">
<xsl :output method="text"/>
</xsl>

Set the output method to text so you are sure that you get a literal result and not valid XML.


Strip white-space an every node, because the serialize function cannot handle that.

<xsl :variable name="total"
select="count(//entry)"/>

Count all node values that we need in the PHP array.

<xsl :template match="/">
</xsl><xsl :text>a:</xsl>
<xsl :value-of select="$total"/>
<xsl :text>:{</xsl>
<xsl :apply-templates/>
<xsl :text>}</xsl>

Start the string with “a:”, then the number of key/value pairs.

<xsl :template match="entry">
</xsl><xsl :text>s:</xsl>
<xsl :value-of_
select="string-length(@name)"/>

Count the key characters.

 <xsl :text>:"</xsl>
<xsl :value-of select="@name"/>
<xsl :text>";</xsl>
 

Add the actual key value

 <xsl :text>s:</xsl>
<xsl :value-of_
select="string-length(@value)"/>

Count the key characters

<xsl :text>:"</xsl>
<xsl :value-of select="@value"/>
<xsl :text>";</xsl> 

Add the actual key value.


And we are done.

Transform the XML file with the XSLT file listed above and you get the the serialized string as a result
Call the unserialize function on the transformation result and you get a true PHP array.

Have a look at the source code and the working code

Comments are closed.

Did you like it?
© 2003 - up to today