或多或少,我需要指导以继续前进..
有一个页面显示容器中的每个节点(对容器进行处理)
XML
<archive>
<data id="1111">
<name>Name</name>
<text>Lots of stuff and things</text>
<etc>Etc</etc>
</data>
<data id="2222">
<name>Name</name>
<text>Different stuff and things</text>
<etc>Etc</etc>
</data>
<data id="3333">
<name>Name</name>
<text>More stuff and things</text>
<etc>Etc</etc>
</data>
// and so on
</archive>
此部分采用XML并回显值等。
$master = array_slice($xml_get->xpath('data'), $start_page, 25);
$master = array_reverse($master);
foreach($master as $arc) {
$last_name = $arc[0]->name;
$last_data = $arc[0]->data;
$last_etc = $arc[0]->etc;
// does stuff with values
}
我想做的是有一个搜索字段,该字段使用该搜索关键字并搜索所有子项,然后为每个匹配的节点和子项进行搜索。
老实说,我只是希望对如何实现此目标有一些指导。我知道如何通过id =单独获取一个节点,但是在那之后..需要指导。
答案 0 :(得分:0)
作为快速示例,请使用XPath搜索if
元素(我更改了您提供的示例数据以显示其选择内容的差异)
if (((x >= x_offset && x < x_offset + size2) && // (x is between x_offset and x_offset +size2)
(y == y_offset || y == y_offset + size2 - 1)) // y is equal to either y_offset OR y_offset + size2 - 1
||
((y >= y_offset && y < y_offset + size2) && //(y is between y_offset and y_offset + size2)
(x == x_offset || x == x_offset + size2 - 1))) // x is equal to either x_offset OR x_offset + size2 -1
{
}
输出..
<text>
XPath-$data = '<archive>
<data id="1111">
<name>Name</name>
<text>Lots of stuff and things</text>
<etc>Etc</etc>
</data>
<data id="2222">
<name>Name</name>
<text>Different stuff and things</text>
<etc>Etc</etc>
</data>
<data id="3333">
<name>Name</name>
<text>More stuff and other things</text>
<etc>Etc</etc>
</data>
</archive>';
$xml_get = simplexml_load_string($data);
$textSearch = "stuff and things";
$matches = $xml_get->xpath('//data[contains(text,"'.$textSearch.'")]');
foreach($matches as $arc) {
echo "text=".$arc->text.PHP_EOL;
}
基本上是说要找到任何具有text=Lots of stuff and things
text=Different stuff and things
元素的//data[contains(text,"'.$textSearch.'")]
元素,该元素的值包含要搜索的字符串。您只需更改<data>