【发布时间】:2021-11-15 16:33:09
【问题描述】:
我有一个案例,我需要解析如下所示的“产品规范”XML 样式文档(数据):
<Message>
<EntityType>Document</EntityType>
<ProductSETS>
<Entity>
<id>1</id>
<Specification1>Human readable title 1</Specification1>
<Specification2>Human readable title 2</Specification2>
<Price>Product Price</Price>
[...more elements of same row amount...]
</Entity>
<Entity>
<id>100</id>
<Specification1>Red</Specification1>
<Specification2>Square</Specification2>
<Price>8888</Price>
[...more elements of same row amount...]
</Entity>
<Entity>
<id>101</id>
<Specification1>Blue</Specification1>
<Specification2>Round</Specification2>
<Price>9999</Price>
[...more elements of same row amount...]
</Entity>
</ProductSETS>
</Message>
简而言之,我需要在值得信赖的 PHP 的帮助下,打印一个包含 XML 数据的表格,例如:
_____________________________________________________________________
| ProductSET for ID 100 |
---------------------------------------------------------------------
| Human readable title 1 | Red |
| Human readable title 2 | Square |
| Product Price | 8888 |
| Next element in human readable form | ID. 100 ELEMENT [...] |
[ .... and loop until end of <Entity> <id>100</id> ............. |
_____________________________________________________________________
| ProductSET for ID 101 |
---------------------------------------------------------------------
| Human readable title 1 | Blue |
| Human readable title 2 | Round |
| Product Price | 9999 |
| Next element in human readable form | ID. 101 ELEMENT [...] |
[ .... and loop until end of <Entity> <id>101</id> ............. |
_____________________________________________________________________
请注意,xml 始终将第一个实体作为类似“标题”的条目,其中 1 用于映射产品规范标题的“人类可读”值 并在“标题”实体之后显示实际的产品规格值(颜色、尺寸、价格等)
问题是我需要两种方法来显示带有来自 XML 数据的值的 HTML 表格:
列出所有(没有实体为 1,但仍然使用它的值)
foreach ($feed->ProductSETS->Entity) :
if(ProductSETS->Entity->id != "1"){
<tr><th><? echo ProductSETS->Entity->[1]->Specification1 ?>:</th>
<td><? echo ProductSETS->Entity->[100]->Title->Value ?> (Red)</td> </tr>
<tr><th><? echo ProductSETS->Entity->[1]->Specification2 ?>:</th>
<td><? echo ProductSETS->Entity->[100]->Title->Value ?> (Square)</td> </tr>
<tr><th><? echo ProductSETS->Entity->[1]->Specification1 ?>:</th>
<td><? echo ProductSETS->Entity->[101]->Title->Value ?> (Blue)</td>
</tr>
<tr><th><? echo ProductSETS->Entity->[1]->Specification2 ?>:</th>
<td><? echo ProductSETS->Entity->[101]->Title->Value ?> (Round)</td> </tr>
}
endforeach;
如果 $filter = "101" 仅针对产品 ID 101 绘制表格但仍使用来自 id 的数据的选择性列表。 1 显示单个 ID 101 产品的人类可读产品规格:
foreach ($feed->ProductSETS->Entity) :
if(ProductSETS->Entity->id == "101"){
<tr><th><? echo ProductSETS->Entity->[1]->Specification1 ?>:</th>
<td><? echo ProductSETS->Entity->[101]->Title->Value ?> (Blue)</td> </tr>
<tr><th><? echo ProductSETS->Entity->[1]->Specification2 ?>:</th>
<td><? echo ProductSETS->Entity->[101]->Title->Value ?> (Round)</td> </tr>
}else{ echo "No matching product search criteria"; }
endforeach;
我认为我的“解释技巧”是最好的,对于“故事”的长度感到抱歉,希望有人可以帮助我。
【问题讨论】:
标签: php html-table xml-parsing