使用php中的simplexml,使用来自子元素的数据来选择其他元素中的数据


using data from child element to select data in other element using simplexml in php

因此,我在php中使用simplexml将xml文件中的元素显示到网页上。当在某个特定元素中选择一个完整列表的子元素时,这可以很好地工作。然而,问题是,我需要在一个元素中选择一些规则,并显示附加在另一个元素的这些特定规则上的数据。

为了澄清,这是我的xml文件的简化版本(它包含了我需要的一切):

<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>

到目前为止,我已经能够在php中使用simplexml用display_name和display_comment来回显整个规则列表。

然而,这不是我想要的。我希望能够只显示实际给出错误的规则的子元素,如根元素"results"中所示。

总结一下我的问题:取<results>中出现错误的规则的id,并使用它们来显示另一个根元素<rules>中显示的display_name和display_comment。

这是我迄今为止所拥有的php代码,列出了整个有效的规则列表,如果这有任何帮助的话。(这是有效的,但这不是我想要的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>';
?>

提前感谢!

使用SimpleXMLElement::xpatharray_map函数的解决方案:

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

"视图源"输出:

        <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>