duanbodai5166 2014-01-23 20:31
浏览 62
已采纳

PHP SimpleXML缺少属性

This isn't a dupe question. Others are missing the attributes in print_r. But I can't access the attribute xlink:href at all.

Here's what i've tried:

        $xml = simplexml_load_string($imageSVG);           
        $image = $xml->g->image; // works
        $style = $xml->g->image->style; // works
        $style = $xml->g->image['style']; // works
        $remoteHref = $xml->g->image['xlink:href']; // doesn't work
        $remoteHref = $xml->g->image['href']; // doesn't work
        $remoteHref = $xml->g->image->href; // doesn't work
        $array= $xml->g->image->attributes('xlink'); // 0 elements in the array

Here's the inputted XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="351" height="351" xml:space="preserve"><desc></desc><defs></defs><g transform="translate(145 175) scale(0.4 0.4)"><image xlink:href="http://cdn.katori.com/BfFEEXuBTrii818CNQvN_71344PL.jpg" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); opacity: 1;" transform="translate(-300 -300)" width="600" height="600" preserveAspectRatio="none"></image></g></svg>

Thanks! -matt

  • 写回答

2条回答 默认 最新

  • duanqiang2617 2014-01-23 21:41
    关注

    The attributes method requires the full namespace, not just the prefix:

    $s = '...';
    
    $xml = new SimpleXMLElement($s);
    
    $attributes = $xml->g->image->attributes('http://www.w3.org/1999/xlink');
    echo $attributes['href'];
    

    Or use the second parameter of attributes (is_prefix - boolean) and specify the prefix only:

    $attributes = $xml->g->image->attributes('xlink', true);
    

    Here it is in action.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么