在 php 中使用 simplexml 使用子元素中的数据到其他元素中的 select 数据

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

因此,我在 php 中使用简单 xml 将 xml 文件中的元素显示到网页上。当 select 在一个特定元素中使用整个列表子元素时,这工作正常。 然而,问题是,我需要在一个元素中 select 一些规则,并显示附加到在另一个元素中找到的这些特定规则的数据。

澄清一下,这是我的 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.[=16 中的简单 xml 来回显整个规则列表及其 display_name 和 display_comment =]

但这不是我想要的。我希望能够仅显示实际出错的规则的子元素,如根元素 "results".

中所示

总结一下我的问题:获取 <results> 中出错的规则的 ID,并使用它们显示 [=15= 中显示的 display_name 和 display_comment ], 另一个根元素。

这是我目前拥有的 php 代码,用于列出所有有效的规则列表(如果有帮助的话)。 (这行得通,但这不是我想要的)

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

"view-source"输出:

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