【问题标题】:using data from child element to select data in other element using simplexml in php使用来自子元素的数据在 php 中使用 simplexml 选择其他元素中的数据
【发布时间】:2016-11-19 19:50:54
【问题描述】:

所以,我在 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 来回显整个规则列表。

但这不是我想要的。我希望能够仅显示实际给出错误的规则的子元素,如根元素“结果”中所示。

总结一下我的问题:获取在&lt;results&gt; 中出现错误的规则的ID,并使用它们来显示另一个根元素&lt;rules&gt; 中显示的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>';
?>

提前致谢!

【问题讨论】:

  • 显示最终结果的外观
  • 报告页面现在看起来像这样:imgur.com/VpAALxC 这正是我希望它看起来的样子(显然减去了样式)但列表现在包含 我需要的所有元素列表仅限于 中显示的 id
  • 我的意思是,最终的 xml 结构应该怎么看,而不是网页输出
  • xml 结构不会改变。我只使用 xml 文件从中获取数据并将其显示在页面上

标签: php xml report simplexml preflight


【解决方案1】:

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

【讨论】:

  • 这不起作用,不确定代码有什么问题,它只是不显示任何内容。我正在单独测试你的代码,所以远离我所有的其他代码。这对你有用吗?
  • 检查您的代码是否有拼写错误。这对我来说可以。我提供的“view-source”输出是 xml 文件的简化版本 的实际输出
  • 我忘了在我的 xml 中添加一个根元素,我刚刚对其进行了编辑,因此它包含了一个。也许这是造成问题的原因?检查错字,仍然无法正常工作。我复制粘贴了您的确切代码并维护了原始文件夹结构
  • @SanderDult,为了比较,尝试将 xml 内容从文件复制到字符串中,并从带有 $xml = simplexml_load_string($str); 的字符串中加载 xml
  • 好的,现在可以了。问题必须在我对 xml 文件的引用中。我会调查一下并报告
猜你喜欢
  • 2015-04-03
  • 1970-01-01
  • 2013-08-23
  • 2010-12-16
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多