我正在尝试存储文本/字符串值。值没有唯一标识符。但是存在一个独特的标签。
使用xPath和PHP,如何存储值?
我的第二个问题是扇区标签包含无,一个或多个结果,具体取决于记录
示例:
[Location] = London, UK;
[Price] = £5000;
[Sector] = IT, ICT;
HTML:
<div class="DetailsPanel">
<label class="ListLabel left">Location</label>
<span id="location" class="ListDetail left" title="London, UK">London, UK</span>
<label class="ListLabel left">Price</label>
<span id="price" class="ListDetail left" title="£5000">£5000</span>
<label class="ListLabel left">Sector</label>
<span class="ListDetail left">
<a href="/">IT</a>
<a href="/">ICT</a>
</span>
</div>
当前代码:
foreach ($entries as $entry) {
$node = $xpath->query("div/a | div/p | div/label | div/span", $entry);
echo '<job>' . "\n";
foreach ($node as $i) {
$tag = $i->nodeName;
$att = $i->getAttribute('id');
$string = $i->nodeValue;
$string = preg_replace('/\s+\s+/','',$string);
....
echo '<' . $tag . ">" . $string . '</' . $tag . ">" . "\n";
答案 0 :(得分:1)
PHP不是我的主要语言,但这里有一个快速的方法,首先获取标签,然后使用xpath获取相邻的span元素:
<?php
$string = <<<XML
<div class="DetailsPanel">
<label class="ListLabel left">Location</label>
<span id="location" class="ListDetail left" title="London, UK">London, UK</span>
<label class="ListLabel left">Price</label>
<span id="price" class="ListDetail left" title="£5000">£5000</span>
<label class="ListLabel left">Sector</label>
<span class="ListDetail left">
<a href="/">IT</a>
<a href="/">ICT</a>
</span>
</div>
XML;
$xml = new SimpleXMLElement($string);
/* get label nodes*/
$label = $xml->xpath('label');
/* iterate over labels */
foreach ($label as $l) {
/* get adjacent span element */
foreach ($l->xpath("following-sibling::span[1]") as $span) {
$a = "";
/* if span has a */
if ($span->xpath("a")) {
$a = join(", ",$span->xpath("a"));
}
}
echo $l, " : ", $span,$a, "<br/>";
}
?>
这将呼应:
Location : London, UK
Price : £5000
Sector : IT, ICT