1

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

Community
  • 1
  • 1
Sander Dult
  • 35
  • 1
  • 6
  • addreses: `http://www.callassoftware.com/namespace/pi4 pi4_results_schema.xsd` and `http://www.callassoftware.com/namespace/pi4` are invalid and return `Not found` – RomanPerekhrest Jul 17 '16 at 20:11
  • that's what I noticed aswel, it's making my entire xml fail to read, I'm not really sure how to approach this issue – Sander Dult Jul 17 '16 at 20:27
  • @RomanPerekhrest Namespace identifiers don't have to actually resolve anywhere. They're URIs just as a way of stopping people all using the same names. – IMSoP Jul 19 '16 at 09:07

1 Answers1

3

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);
har07
  • 88,338
  • 12
  • 84
  • 137