【发布时间】:2010-07-26 19:43:10
【问题描述】:
我正在使用 SimpleXMLElement 和 xpath 尝试从最底部的 xml 中读取 <subcategory><name>。这段代码有效.. 但while 循环内的东西看起来有点乱,现在我也想得到<subcategory><count> 并以某种方式将它与适当的<subcategory><name> 配对。
$names = $xml->xpath('/theroot/category/subcategories/subcategory/name/');
while(list( , $node) = each($names)) {
echo $node;
}
我的问题:是否有可能在仍然使用 xpath 的同时获得这种配对,因为它看起来可以让工作更轻松?
<theroot>
<category>
<name>Category 1</name>
<subcategories>
<subcategory>
<name>Subcategory 1.1</name>
<count>18</count>
</subcategory>
<subcategory>
<name>Subcategory 1.2</name>
<count>29</count>
</subcategory>
</subcategories>
</category>
<category>
<name>Category 2</name>
<subcategories>
<subcategory>
<name>Subcategory 2.1</name>
<count>18</count>
</subcategory>
<subcategory>
<name>Subcategory 2.2</name>
<count>29</count>
</subcategory>
</subcategories>
</category>
</theroot>
【问题讨论】: