-1

So, I'm using simplexml in php to display elements from an xml file onto a webpage. This is working fine when selecting a whole list child elements in one certain element. The thing is however, I need to select a number of rules in one element, and display the data attached to these certain rules found in another element.

To clarify, this a simplified version of my xml file (it contains everything I need):

<report>
    <profile_info>  
    <rules>

        <rule id="RUL1">
            <display_name>
            XMP does not have CreateDate entry in the XMP Basic Schema
            </display_name>
            <display_comment>
            This entry of the predefined XMP Basic Schema defines the date and time when the document has been created. It is required by some PDF-based ISO standards.
            </display_comment>
        </rule>

        <rule id="RUL133">
            <display_name>
            XMP does not have a VersionID entry in the XMP Media Management Schema
            </display_name>
            <display_comment>
            The VersionID in the XMP Media Management Schema (xmpMM) is the version identifier for respective resource. This entry is required by some PDF-based ISO standards for file identification.
            </display_comment>
        </rule>

        <rule id="RUL169">
            <display_name>
            Page does not have TrimBox or ArtBox
            </display_name>
            <display_comment>
            Either ArtBox or TrimBox should be defined for pages in a PDF file that are used in prepress. PDF/X ISO standards require the presence of one of these boxes.
            </display_comment>
        </rule>

    </rules>
</profile_info>


<results>
    <hits rule_id="RUL169" severity="Error">
    </hits>
    <hits rule_id="RUL133" severity="Error">
    </hits>
</results>
</report>

So far, I'm able to echo the entire list of rules with their display_name and display_comment using simplexml inside of php.

This is not what I want however. I want to be able to only show the child elements of the rules that actually gave an error, as displayed in the root element "results".

To sum up my problem: take the id's of the rules that gave an error in <results> and use those to display the display_name and display_comment shown in <rules>, another root element.

This the php code I have so far to list the entire list of rules that works, if that is of any help. (this works, but this is not what I want btw)

<?php

    if(file_exists('../uploads/reports/report.xml')){
    $xml = simplexml_load_file('../uploads/reports/report.xml');
    } 
    else{
    exit('Could not load the file ...');
    }

foreach ($xml->profile_info as $profile_info){
    foreach ($profile_info->rules as $rules){
        foreach ($rules->rule as $rule){

                echo '<div id="name">'.$rule->display_name.'</div>'.'<div id="comment">'.$rule
                    ->display_comment.'</div>';
?>

Thanks in advance!

Sander Dult
  • 35
  • 1
  • 6

1 Answers1

1

The solution using SimpleXMLElement::xpath and array_map functions:

$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>';
}

The "view-source" output:

        <div id="name">
        XMP does not have a VersionID entry in the XMP Media Management Schema
        </div><div id="comment">
        The VersionID in the XMP Media Management Schema (xmpMM) is the version identifier for respective resource. This entry is required by some PDF-based ISO standards for file identification.
        </div><div id="name">
        Page does not have TrimBox or ArtBox
        </div><div id="comment">
        Either ArtBox or TrimBox should be defined for pages in a PDF file that are used in prepress. PDF/X ISO standards require the presence of one of these boxes.
        </div>
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • this does not work, not sure what is wrong in the code, it just doesn't display anything. I'm testing your code separately, so away from all of my other code. Does this work for you? – Sander Dult Jul 17 '16 at 07:30
  • check your code for typos and errors. It works fine for me. The "view-source" output I've presented is the actual output for the *simplified version of your xml file* – RomanPerekhrest Jul 17 '16 at 07:57
  • I forgot to add in a root element in my xml, I edited it just now so it includes one. Maybe that was causing the problem? Checked for typo's, still can't get it to work. I copy-pasted your exact code and maintained the original folder structure – Sander Dult Jul 17 '16 at 08:11
  • @SanderDult, for comparison, try to copy the xml content from file into string and load xml from a string with `$xml = simplexml_load_string($str);` – RomanPerekhrest Jul 17 '16 at 08:19
  • Okay, this works now. The problem must lay within my reference to the xml file then. I'll have a look into it and report back – Sander Dult Jul 17 '16 at 08:27
  • So I reconstructed the path to my xml file and now it works fine from the xml file. Don't know what went wrong earlier with this though. I'll unleash this now on my huge xml file and will report back. Thanks for being patient with me, great help! – Sander Dult Jul 17 '16 at 08:33