douguanya6399 2016-07-17 18:40
浏览 68
已采纳

root中的属性导致xml文件无法通过simplexml php加载

So, this is a follow-up question to my previous question that was solved, here's the link to it: using data from child element to select data in other element using simplexml in php

thanks to @RomanPerekhrest for solving this.

I have this piece of php code using simplexml to read my xml file

<?php



    $xml = simplexml_load_file('../uploads/reports/report.xml');

$hits = $xml->xpath("results/hits/@rule_id");
$ruleIds = array_map(function($v){    // getting search path for each needed rule
    return "profile_info/rules/rule[@id='". (string)$v. "']"; 
}, $hits);

foreach ($xml->xpath(implode(" | ", $ruleIds)) as $rule) {
    echo '<div id="name">'. $rule->display_name .'</div>'.
         '<div id="comment">'. $rule->display_comment .'</div>';
}                                           
?>

again, thanks to @RomanPerekhrest for coming up with this.

This piece of code works fine with my simplified xml-file I created to illustrate my problems in my previous questions, but when I apply it, it doesn't seem to render.

I've found the reason why, in my root element there are some xmlns attributes that cause my xml not to load. When I manually remove these attributes, everything works as expected. (I will not the list the entire xml document, since it is 8500+ lines long) Here is the root element with the attributes:

<report xsi:schemaLocation="http://www.callassoftware.com/namespace/pi4 pi4_results_schema.xsd" xmlns="http://www.callassoftware.com/namespace/pi4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I need a way to bypass in php. Seeing as these xml files are generated by other software and the lack of settings in this generation, I cannot simply make these xml files without these attributes.

Thanks

  • 写回答

1条回答 默认 最新

  • duanlanzhi5509 2016-07-18 04:18
    关注

    Your XML has default namespace declared at the root element, which descendant elements without prefix inherit implicitly :

    xmlns="http://www.callassoftware.com/namespace/pi4"
    

    To reference element in default namespace, you need to map a prefix to the default namespace URI, and then use that prefix in your XPath :

    //register prefix 'd' to reference default namespace URI
    $xml->registerXPathNamespace('d', 'http://www.callassoftware.com/namespace/pi4');
    
    //use the prefix to reference elements in the default namespace
    $hits = $xml->xpath("d:results/d:hits/@rule_id");
    $ruleIds = array_map(function($v){    // getting search path for each needed rule
        return "d:profile_info/d:rules/d:rule[@id='". (string)$v. "']"; 
    }, $hits);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译