<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Thijs Lensselink&#039;s Blog &#187; XML</title>
	<atom:link href="http://lenss.nl/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://lenss.nl</link>
	<description>Webdevelopment and stuff...</description>
	<lastBuildDate>Thu, 26 Apr 2012 21:48:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>PHP5 SimpleXML and xpath</title>
		<link>http://lenss.nl/2008/08/php5-simplexml-and-xpath/</link>
		<comments>http://lenss.nl/2008/08/php5-simplexml-and-xpath/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 12:55:27 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SimpleXML]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://we.designandco.de/?p=50</guid>
		<description><![CDATA[Yesterday when working with some rather large XML files i noticed. That it&#8217;s not possible to do -&#62;xpath() calls on sub nodes of a SimpleXMLElement. If that&#8217;s not clear. Here is a small example. $xmlStr = '&#60;root&#62; &#60;fareOption&#62; &#60;fare&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;/fare&#62; &#60;/fareOption&#62; &#60;fareOption&#62; &#60;fare&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;/fare&#62; &#60;/fareOption&#62; &#60;/root&#62;'; $objXML = simplexml_load_string($xmlStr); [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday when working with some rather large XML files i noticed. That it&#8217;s not possible to do -&gt;xpath() calls on sub nodes of a SimpleXMLElement. If that&#8217;s not clear. Here is a small example.</p>
<pre name="code" class="php">$xmlStr = '&lt;root&gt;
	&lt;fareOption&gt;
		&lt;fare&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
		&lt;/fare&gt;
	&lt;/fareOption&gt;
	&lt;fareOption&gt;
		&lt;fare&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
		&lt;/fare&gt;
	&lt;/fareOption&gt;
&lt;/root&gt;';

$objXML = simplexml_load_string($xmlStr);
$fareList = $objXML-&gt;xpath('//fareOption');

foreach ($fareList as $fare) {
    $flightList = $fare-&gt;xpath('//flight');
    print_r($flightList);
}</pre>
<p>So first i try to get all the fareOption elements from the XML structure. And loop through the extracted elements. In the loop i want to do a -&gt;xpath call on $fare which is a instance of SimpleXMLElement. The result however is not what i expected. Instead of extracting all the flights of a $fare node. It extracts all flights from the root of the document. So instead of 3 flight nodes i get 6.</p>
<p>I couldn&#8217;t really find a solution for this problem. And from reading some bug reports i understand this is how the PHP implementation of xpath works at the moment. To solve this problem. There is a small work around though. Instead of running the -&gt;xpath() call on the $fare node directly. We can create a new SimpleXMLElement from the $fare node. And do a -&gt;xpath() call on that. This will look something like this.</p>
<pre name="code" class="php">foreach ($fareList as $fare) {
    $tempList = simplexml_load_string($fare-&gt;asXML());
    $flightList = $tempList-&gt;xpath('//flight');
    print_r($flightList);
}</pre>
<p>This creates the expected output.</p>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2008/08/php5-simplexml-and-xpath/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
