我有一个XML文件,看起来像这样:
<rss>
<channel>
<title>title</title>
<link>link</link>
<description>description</description>
<item>
<title>title 1</title>
<sort>1</sort>
</item>
<label>
<title>LABEL 1</title>
<sort>2</sort>
</label>
<item>
<title>title 2</title>
<sort>3</sort>
</item>
<label>
<title>LABEL 2</title>
<sort>4</sort>
</label>
<item>
<title>title 3</title>
<sort>5</sort>
</item>
<template>3</template>
</channel>
</rss>
我使用SimpleXML,我需要在'item'和'label'节点的组合列表上执行foreach,忽略'channel'子节点的其余部分。
我正在使用:
foreach($feed->channel->item as $item) { }
…标签添加为:
<item type="label">
<title>LABEL 1</title>
</item>
…但是我正在构建的文件的消费者正在等待标签的节点。
<人力资源>人力资源>
更新!
由于问题没有被发布(需要一个标题!),我在此期间自己找到了答案,以下是我所做的:
/* Search for items and labels */
$result = $iFeed->xpath('channel/*[name()="item" or name()="label"]');
foreach($result as $item) {
// boom
}
我认为您也可以使用|
运算符。这将合并多个XPath查询的结果。
$result = $root->xpath('channel/item|channel/label');
看起来它还保留了节点的顺序,所以节点按以下顺序返回:"item, label, item, label, item"(而不是"item, item, item, label, label")。