【发布时间】: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 来回显整个规则列表。
但这不是我想要的。我希望能够仅显示实际给出错误的规则的子元素,如根元素“结果”中所示。
总结一下我的问题:获取在<results> 中出现错误的规则的ID,并使用它们来显示另一个根元素<rules> 中显示的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