xpath无法获取其父名称出现在同级

时间:2015-08-28 13:47:51

标签: php xml xpath

我有一个xml结构:



<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>
&#13;
&#13;
&#13; 我发出了一个xpath表达式来获取文档中的所有菜单节点, 其父名在父母的任何兄弟中出现(或列出) 在同一份文件中。尝试了两种不同的xpressions:

&#13;
&#13;
$PageName = 'Home';

"./*/*[name() = 'menu' and ../../$this->PageName/menu/item/. = name(..)]";

and 

"./*[name() = (following-sibling::$this->PageName | preceding-sibling::$this->PageName)/menu/*]/menu";
&#13;
&#13;
&#13;

在任何一种情况下,我在var_dump()之后得到的结果是:

&#13;
&#13;
'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 something 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)

      ...
&#13;
&#13;
&#13; 为什么xpath只返回一个菜单数组(帮助/菜单) 拒绝复制关于/菜单,联系人/菜单等。或者我 我做错了什么?任何帮助将不胜感激。 thaks。

0 个答案:

没有答案