dqxafj6830 2011-01-18 14:37
浏览 91
已采纳

使用变量用PHP-XPath解析xml提要

I have a problem in using PHPXPath to fetch exchange rates based on the country of a logged-in client.

For reference: PHP XPath is a php class for searching an XML document using XPath.

I have a database with all the clients nations and associated currency values.

The code I'm using so far to fetch the rates (from ECB feed) is this:

$Rates = new XPath();
$Rates->importFromFile("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"); 
$userRate = $Rates->getAttributes("//Cube[@currency='USD']","rate"); 

Now, what I want is to pass a variable as the currency value (USD in the example above). My problem is, since I'm completely new to XPath, is the syntax to do that. Assuming the variable name is

$user_data->GRUPPO_005

I've tried the following solutions, but I keep getting "UNEXPECTED T_VARIABLE" error:

$userRate = $Rates->getAttributes("//Cube[@currency='"$user_data->GRUPPO_005"']","rate"); 
$userRate = $Rates->getAttributes("//Cube[@currency='".$user_data->GRUPPO_005."']","rate"); 
$userRate = $Rates->getAttributes("//Cube[@currency='.$user_data->GRUPPO_005.']","rate"); 

I think this is because of my scarce knowledge of the language, I'd love a small hint of this.

  • 写回答

2条回答

  • duanaidang6197 2011-01-18 14:42
    关注

    Ok, I have no clue what PHPXPath is but since you seem to have trouble assembling a string, try

    $Rates->getAttributes(
        sprintf('//Cube[@currency="%s"]', $user_data->GRUPPO_005),
        'rate'
    );
    

    See http://us3.php.net/manual/en/function.sprintf.php

    On a sidenote, there is a PEAR Package for the ECB rates, so you could save yourself some trouble writing your own query tool by just using that instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波