当前位置: 代码迷 >> PHP >> php xPath 解析xml文件 有关问题
  详细解决方案

php xPath 解析xml文件 有关问题

热度:696   发布时间:2016-04-29 00:03:22.0
php xPath 解析xml文件 问题
说来话长-为了描述的好一些
从前有个xml 文件, 文件内容大致如下:
<root>
 <collection id="co1">
  <category id="ca1">aaa</category>
  <category id="ca2">bbb</category>
 </collection>
 <collection id="co2">
  <category id="ca3">ccc</category>
  <category id="ca4">ddd</category>
 </collection>
</root>
很明了,就是可以有若干个collection, 并且在每个collection里包含若干个category.并且collection和category的id都是唯一的, 以前我在修改category的node value的时候. 一直是用xpath 例如:把id为ca1的category的值改掉,我就可以
$xpath->query("/root/collection/category[@id='"ca1"']"); 之后定位 修改就可以了。

但是,注意但是了啊!转折了啊!
但是现在xml文件的格式变化了 变为:
<root>
 <collection id="co1">
  <category id="ca1">
  <category id="ca3">
  <category id="ca4">aaa</category>
  </category>
  </category>
   
  <category id="ca2">bbb</category>
 </collection>
</root>
就是在以前的基础上改为 一个category里面还可以有 N 个 category, 所以这种情况 如果改某个特定的category的值的话 我就不会了。比如 把 id为 ca4的 category的值 改为 xxx, 我就不知道了。
我想还用 xpath 但不知道怎么用 
$xpath->query("/root/collection/在这我就不知道应该怎么写了/category[@id='"aaa"']"); 

像以前我知道collection下面一定是category而且只有一层,所以我可以很容易定位。现在我就不会了。请大家帮助我。谢谢。

------解决方案--------------------
/root/collection//category[@id='aaa']

这样.