2011-08-07 56 views
0

我是Xpath的新手,實際上只是今天第一次看到它,並且我在XPath中有一些問題,沒有加載在XML中定義的圖像。XML XPath問題

到目前爲止,我有:

<page name = "home"><br /> 
<image> 
     <imagehome1> 
      <imge>resp.jpg</imge><br /> 
      <class>right</class> 
     </imagehome1> 
     <imagehome2> 
      <imge></imge><br /> 
      <class>right</class> 
     </imagehome2> 
     <imagehomebot> 
      <imge>cfr.png</imge><br /> 
      <class>bottom</class> 
     </imagehomebot> 
    </image> 
</page> 

正如你可以看到我定義2個圖像,1 imagehome1和imagehomebot 1,但是我的XPath代碼只顯示來自imagehomebot圖像.....和我不知道爲什麼。

我的PHP的XPath代碼:

<? 
       $nodes = $dom->xpath('/content/page/image/imagehome1'); 
       if (count($nodes) > 0) { 
        if(!(string)$nodes[0]->imge){ 
         $class = "";} 
        else{ 
        $class3 = (string)$nodes[0]->class; 
        $img3 = (string)$nodes[0]->imge;} 
       } 
      ?> 
    <div class="<? echo $class ?>"> 
     <img src="imgs/<? echo $img1 ?>" /> 
    </div> 

    <? 
       $nodes = $dom->xpath('/content/page/image/imagehome2'); 
       if (count($nodes) > 0) { 
        if(!(string)$nodes[0]->imge){ 
         $class = "";} 
        else{ 
        $class3 = (string)$nodes[0]->class; 
        $img3 = (string)$nodes[0]->imge;} 
       } 
      ?> 

    <div class="<? echo $class ?>"> 
     <img src="imgs/<? echo $img2 ?>" /> 
    </div> 

    </div> 


    <div id="box2"> 

    <? 
       $nodes = $dom->xpath('/content/page/image/imagehomebot'); 
       if (count($nodes) > 0) { 
        if(!(string)$nodes[0]->imge){ 
         $class = "";} 
        else{ 
        $class3 = (string)$nodes[0]->class; 
        $img3 = (string)$nodes[0]->imge;} 
       } 
      ?> 

<div class="<? echo $class3 ?>"> 
     <img src="imgs/<? echo $img3 ?>" /> 
    </div> 

所有幫助深表感謝。 感謝

編輯:剛剛另一快看在我的代碼,我想我知道爲什麼,該死的複製和粘貼哈哈......

+0

是否存在所有這些圖像? HTML輸出是什麼樣的? – GolezTrol

+0

實際顯示imagehomebot圖像的代碼在哪裏? – GolezTrol

+0

它可能與「imgs」文件夾後面缺少的'/'有關,但由於它現在已經奇蹟般地出現了,您的問題可能會得到解決,對吧? :) – GolezTrol

回答

0

看起來你要處理一個包含所有節點IMGE節點的相同。

嘗試

'/內容/網頁/圖片/ * [IMGE]'

得到imagehome1,imagehome2和imagehomebot一個XPath的。如果你想限制具有實際字符串時,它可能是這樣的

/內容/網頁/圖片/ * [IMGE /文()]

中的表達「[]」是對那些節點「謂詞」指定與其前面的「上下文」節點相關的標準(「*」),它表示獲取圖像的所有子節點。 (我這樣做是因爲它們的命名方式不同。)