使用子元素中的数据在php中使用simplexml选择其他元素中的数据 [英] using data from child element to select data in other element using simplexml in php
问题描述
因此,我正在php中使用simplexml将xml文件中的元素显示到网页上.当在一个特定元素中选择整个列表子元素时,这种方法很好用. 但是,我需要在一个元素中选择许多规则,并显示附加到在另一个元素中找到的这些特定规则的数据.
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.
为澄清起见,这是我的xml文件的简化版本(包含我需要的所有内容):
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>
到目前为止,我能够使用php内部的simplexml来回显规则的整个列表以及它们的display_name和display_comment.
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".
总结一下我的问题:取<results>
中出现错误的规则的id,并使用它们显示另一个根元素<rules>
中显示的display_name和display_comment.
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.
到目前为止,我已经列出了所有可用的php代码(如果有帮助的话). (这可行,但这不是我想要的)
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>';
?>
提前谢谢!
推荐答案
使用SimpleXMLElement::xpath
和array_map
函数的解决方案:
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>';
}
查看源代码"输出:
<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>
这篇关于使用子元素中的数据在php中使用simplexml选择其他元素中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!