我试图通过此html代码获取xpath的每个类别的子类别:
<ul class="simple_list">
<li class="category"><span class="category_span">ILUMINAT UTILITAR</span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/indus70">Indus70</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/apliled48">ApliLED48</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/minoris">Minoris</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/canopus_230v">Canopus 230V</a></span></li>
<li class="category"><span class="category_span">ILUMINAT OFFICE</span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/pavo_1_modul">Pavo 1 modul</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/laminar">Laminar</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/alcor_t">Alcor T</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/grup/Cassiopeia">Cassiopeia</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/sirius_8_module">Sirius 8 module</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/cygnus">Cygnus</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/grup/Aries">Aries</a></span></li>
<li class="category"><span class="category_span">ILUMINAT INDUSTRIAL</span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/castor_6_module">Castor 6 module</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/grup/Corona">Corona</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/octans">Octans</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/lacerta_3_module">Lacerta 3 module</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/fornax">Fornax</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/grup/Cetus">Cetus</a></span></li>
<li class="category"><span class="category_span">ILUMINAT DE EXTERIOR</span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/grup/ELMA80">ELMA80</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/solaris_2_module">Solaris 2 module</a></span></li>
<li class="category"><span class="category_span">BECURI CU LED</span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/grup/LEC">LEC</a></span></li>
<li class="category"><span class="category_span">ACCESORII</span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/grup/Banda">Banda</a></span></li>
<li class="subcat"><span class="subcat_span"><a href="/produse/drosel">Drosel</a></span></li>
对于我试过的子项目:
$xpath->query('//li[@class="subcat" and not(preceding-sibling::li[@class="category"]['.$i.'])])
我想提取一个类别项下的所有子项......任何xpath想法都会很棒。
此时我正在这样做:
for ($i = 0; $i<=$nodecount; $i++) {
foreach( $xpath->query('//li[@class="category"]['.$i.']') as $category ) {
echo "<br/>" . "$i - Main Categ Name : " . $category->nodeValue . "<br/>";
//Now get the subcateg names and links
$subnodecount = 0;
foreach( $xpath->query('//li[@class="subcat"]') as $sub_categ_links_on_page ) {
$subnodecount++;
}
for ($y = 0; $y<=$subnodecount; $y++) {
foreach( $xpath->query('//li[@class="subcat" and preceding-sibling::li[@class!="category"]]['.$i.']['.$y.']') as $sub_category ) {
echo "--- $y - Sub Categ Name : " . $sub_category->nodeValue . "<br/>";
}//end foreach
}// end FOR $y
///////////////////////////////////////////////////////////
}//end foreach
}// end FOR $i
但我被困在子分类部分..
我希望得到这个输出:
1 - 主要分类名称:ILUMINAT UTILITAR
--- 1 - 子类别名称:ApliLED48
--- 2 - 子类别名称:Minoris
--- 3 - 子类别名称:Canopus 230V
--- 4 - 子类别名称:Pavo 1 modul
[等等]
现场代码可以找到并摆弄,在这里: http://codepad.viper-7.com/i8hweN
答案 0 :(得分:1)
要获取该类别下方的所有列表,您还可以在这种情况下使用!=
:
$xpath->query('//li[@class="subcat" and preceding-sibling::li[@class!="category"]]');
作为替代方案,您可以先收集所有这些结果。然后你可以使用CSS或简单的格式进行演示:
$elements = $xpath->query('//li[@class="subcat" and preceding-sibling::li[@class!="category"]]');
$list = array();
if($elements->length > 0) {
foreach($elements as $cat) {
$category = $xpath->evaluate('string(./preceding-sibling::li[@class="category"][1])', $cat);
$list[$category][] = $cat->nodeValue;
}
}
// presentation
$i = $j = 1;
foreach($list as $category => $subcategory) {
echo $i . '. ' . $category . '<br/>'; $i++;
foreach($subcategory as $sub) {
echo str_repeat(' ', 5);
echo $j .'. ' . $sub . '<br/>'; $j++;
}
$j = 1;
}