本文介绍了xpath从父级名称在父级兄弟中登记的节点获取菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以向我解释为什么xpath表达式如下:
can someone explain to me, why an xpath expression as this:
/*Never mind the nodeObj, its been successfully instantiated with root node added internally*/
$PageName = 'Home';
$nodeObj->xpath("./*[name() = (following-sibling::$PageName | preceding-sibling::$PageName)/menu/*]/menu");
将返回只有一个菜单项阵列如:
will return just one array of menu item like:
'menu' =>
array (size=1)
'item' =>
array (size=3)
0 => string 'Business Help' (length=13)
1 => string 'Web Help' (length=8)
2 => string 'Tour Help' (length=9)
而不是两个菜单项的数组,如:
instead of an array of two menu items like:
'menu' =>
array (size=2)
'item' =>
array (size=3)
0 => string 'Business Help' (length=13)
1 => string 'Web Help' (length=8)
2 => string 'Tour Help' (length=9)
'item' =>
array (size=2)
0 => string 'About us' (length=8)
1 => string 'About our food' (length=14)
以下是xml结构的摘录:
Here is an extract of the xml stucture:
<Pages> <!--This is the root node-->
<Home>
<url>...</url>
<menu>
<item>Help</item>
<item>About</item>
<item>Contact</item>
</menu>
</Home>
<Help>
<url>...</url>
<menu> <!--i need to select this if its parent(Help) is listed in Home/menu/item above-->
<item>Business Help</item>
<item>Web Help</item>
<item>Tour Help</item>
</menu>
</Help>
<About>
<url>...</url>
<menu> <!--And Or select this if its parent(About) is listed in Home/menu/item above-->
<item>About us</item>
<item>About our food</item>
</menu>
</About>
<Contact>
<url>...</url>
<menu> <!--And Or select this if its listed in Home/menu/item above-->
...
and so on...
...
</menu>
</Contact>
</Pages>
这里是一段试图操纵它的php代码
and here's a snippet of the php code that attempts to maniplate it
/*
Here's a snippet of the class from from
which $nodeObj was stencilled :
class XMLManager {
public static $xmlObj = null; //internal object
public $Err = array(); //Error code
public function __construct($rootName, $InitVersion = false, $PathToFile = '') {
if(!$this->Create( $rootName, $InitVersion, $PathToFile ))
throw new XMLException($this->Err); //Create sets $this->Err on failure
}
//now, here's method Create()
private function Create( $modelName, $VersionInit, $PathToFile = '') {
//turn off compatibility mode cause simple xml throws a wobbly if you don't.
if ( ini_get('zend.ze1_compatibility_mode') == 1 ){
ini_set ('zend.ze1_compatibility_mode', 0);
}
libxml_use_internal_errors(true);
if($this->getDOMObj($VersionInit, $PathToFile)) {
return self::CreateRootNode(self::$xmlObj, $modelName); basically calls on DOMDocument::createElement to add root (Pages in this case) to my dom.
}
return false;
}
public function getDOMObj($VersionInit = false, $PathToFile = ''){
if(empty(self::$xmlObj)){
self::$xmlObj = ($VersionInit == false) ? new DOMDocument : new DOMDocument('1.0', 'UTF-8');
if(self::$xmlObj == null){
$this->parseError(); //this parses internal xml error and assigns outcome to $this->Err
return false;
}
return (isset($PathToFile)) ? $this->Load(self::$xmlObj, $PathToFile) : true;
}
//echo 'success ';
return self::$xmlObj;
}
}
usage:
so, when i created $nodeObj like this:
$nodeObj = new XMLManager('Pages', false, 'myPages.xml');
what i got was actually Pages (root node) //that just what i liked
supposedly,
$nodeObj->xpath("./*[...]"); //as above
should select from current root node (Pages);
Note that xpath() is a custom method in XMLManager that returns feched result
as array. mind you, it only returns what has been fetched by xpath in array
form.
*/
我做错了什么?任何帮助将不胜感激。
What am i doing wrongly ? any help will be greatly appreciated.
推荐答案
这篇关于xpath从父级名称在父级兄弟中登记的节点获取菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!