Welcome to WebmasterWorld Guest from 54.90.204.233

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Xpath and currency converting drop down.

How to use xpath to add currency data to a drop down

     
12:02 pm on Jun 19, 2008 (gmt 0)

New User

10+ Year Member

joined:June 19, 2008
posts: 5
votes: 0


Hi

I found this snippet elsewhere on this forum, but I'd like to know if it is possible not to show just ONE currency, but use a similar method to get ALL of the country names and rates into a drop down like so..

<select name="rates">
<option value="1.5">GBR</option>
<option value="2.5">EUR</option>
..and so on..
</select>

I'd really appreciate help on this, I've been trying to get something like this working for almost 2 days!

The XML file is here:
[ecb.int...]

----xpath code below shows just one rate---

<?php

require("XPath.class.php");

$xp = new XPath();

$xp->importFromFile("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");

$EUR_GBP_RATE = $xp->getAttributes("//Cube[@currency='GBP']","rate");

echo($EUR_GBP_RATE);

?>

12:41 pm on June 19, 2008 (gmt 0)

New User

10+ Year Member

joined:June 19, 2008
posts: 5
votes: 0


I am probably way off the mark, but this is kind of the direction I 'think' it should go..

<?php

require("XPath.class.php");

// a local copy of the xml (for now)
$file = "eurofxref-daily.xml";

// load file
$xp = simplexml_load_file($file) or die ("Unable to load XML file!");

//try and find all of the country codes and their rates (my code here is wrong, I am sure of it)
$results = $xp->xpath('//Cube/currency');

// hopefully you can see what I am trying to do here
foreach($results as $result) {
echo $result."<br>";
}

?>

I just have no experience with XML

7:19 pm on June 20, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


You can reference the objects directly:
$xml = simplexml_load_file('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml'); 
$options = '';
foreach ($xml->Cube->Cube->Cube as $cube) {
$options .= "<option value=\"{$cube['rate']}\">{$cube['currency']}</option>\n";
}
print "<select name=\"rates\">{$options}</select>";

I tried using xpath, but I believe it fails because of the invalid namespace for that resource. This won't work ...
foreach ($xml->xpath('//Cube[@time]/Cube') as $cube) { 
$options .= "<option value=\"{$cube['rate']}\">{$cube['currency']}</option>\n";
}

... unless the namespace is removed. What I mean is I loaded the xml as a string and removed the bold part:

<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">

and then using xpath worked just fine.

8:16 pm on June 20, 2008 (gmt 0)

New User

10+ Year Member

joined:June 19, 2008
posts: 5
votes: 0


That is perfect! How elegantly simple, I can't thank you enough. I was tripping over with the path. Your explanation has helped me understand a huge chunk of what I didn't know about XML.

Thanks again.

Mike :)

8:24 pm on June 20, 2008 (gmt 0)

New User

10+ Year Member

joined:June 19, 2008
posts: 5
votes: 0


If it helps anyone else, this is a part of the code that this all went into. Keep in mind that ECB's XML is all rates relative to the euro. So, this snippet includes EUR as the first line a default selected item.

<form name="fxform" method="post" action="">
1 x
<select name="rates" id="rates" onchange="this.form.elements['rate'].value=this.options[this.selectedIndex].value;">
<option value="1" selected="selected">EUR (default)</option>
<?php
$xml = simplexml_load_file('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
$options = '';
foreach ($xml->Cube->Cube->Cube as $cube) {
$options .= "<option value=\"{$cube['rate']}\">{$cube['currency']}</option>\n";
}
print "{$options}";
?>
</select>
=
&euro;
<input name="rate" type="text" id="rate" value="" size="10">
</form>

8:26 pm on June 20, 2008 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


Well, I must admit I am confused on one issue here yet as well in regards to the xpath. It fails on me unless I peel out the namespace as I said. Otherwise, using xpath is ideal. The foreach I threw down there merely says "find any descendant named Cube that has a 'time' attribute and iterate over it's descendant named Cube."

BTW, do you think they used the Cube namespace enough in that XML? :P

11:08 pm on June 20, 2008 (gmt 0)

New User

10+ Year Member

joined:June 19, 2008
posts: 5
votes: 0


I dropped xpath as fast as you did in your example. I did get it working using xpath, but with horrible bloated code.

With your help, the final version doesn't use xpath at all and may I say it is MUCH faster now too! :)

Yes, the multiple use of 'code' is what was tripping me. I didn't know if I had to use all of the descendant name i.e"<Cube time='2008-06-17'>", but now I know :)

If you lived closer, I'd owe you a beer!

EDIT: in fact, my speciality is graphics.. if you need any logos or site style modernisation or anything, just shout.