2012-03-17 59 views
0

我想知道是否有人能夠幫助我。使用Xpath檢索所選記錄

我正在使用下面的腳本創建一個「圖庫」頁面。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<?php 

    //This variable specifies relative path to the folder, where the gallery with uploaded files is located. 

    $galleryPath = 'UploadedFiles/'; 

    $thumbnailsPath = $galleryPath . 'Thumbnails/'; 

    $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 

    $descriptions = new DOMDocument('1.0'); 
    $descriptions->load($absGalleryPath . 'files.xml'); 

    path="files[userid=1]/originalname/folder/description/source/thumbnail"; 
// code for IE 
if (window.ActiveXObject) 
{ 
var nodes=xml.selectNodes(path); 

for (i=0;i<nodes.length;i++) 
    { 
    document.write(nodes[i].childNodes[0].nodeValue); 
    } 
} 

?> 
<head> 
    <title>Gallery</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <link href="Libraries/fancybox/jquery.fancybox-1.3.1.css" rel="stylesheet" type="text/css" /> 
    <link href="Styles/style.css" rel="stylesheet" type="text/css" /> 
    <!--[if IE]> 
    <link href="Styles/ie.css" rel="stylesheet" type="text/css" /> 
    <![endif]--> 
    <script src="Libraries/jquery/jquery-1.4.3.min.js" type="text/javascript"></script> 
    <script src="Libraries/fancybox/jquery.fancybox-1.3.1.pack.js" type="text/javascript"></script> 
    <script type="text/javascript"> 

    $(function() { $('a.fancybox').fancybox(); }); 

    </script> 
    <style type="text/css"> 
<!-- 
.style1 { 
    font-size: 14px; 
    margin-right: 110px; 
} 
.style4 {font-size: 12px} 
--> 
    </style> 
</head> 
<body style="font-family: Calibri; color: #505050; font-size: 9px; border-bottom-width: thin; margin-top: 5px; margin-left: -475px; margin-right: 1px; margin-bottom: -10px;"> 
<div align="right" class="style1"> <a href = "imagefolders.php" /> View Uploaded Images In Folder Structure <a/> &larr; View All Uploaded Images </div> 
    <form id="gallery" class="page"> 
    <div id="container"> 
    <div id="center"> 
     <div class="aB"> 
     <div class="aB-B"> 
      <?php if ('Uploaded files' != $current['title']) :?> 
      <?php endif;?> 
      <label> 
      <input name="userid" type="text" id="userid" value="1" /> 
      <input name="locationid" type="text" id="locationid" value="1" /> 
      </label> 
      <div class="demo"> 
      <div class="inner"> 
       <div class="container"> 
       <div class="gallery"> 
        <ul class="gallery-image-list"> 
        <?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) : 
          $xmlFile = $descriptions->documentElement->childNodes->item($i); 
          $name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8'); 
          $description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8'); 
          $folder = htmlentities($xmlFile->getAttribute('folder'), ENT_COMPAT, 'UTF-8'); 
          $source = $galleryPath . rawurlencode($xmlFile->getAttribute('source')); 
          $thumbnail = $thumbnailsPath . rawurlencode($xmlFile->getAttribute('thumbnail')); 
        ?> 
        <li class="item"> 
         <a class="fancybox" target="_blank" rel="original" href="<?php echo $source; ?>"><img class="preview" 
         alt="<?php echo $name; ?>" src="<?php echo $thumbnail; ?>" /></a>      </li> 
         <li class="item"></li> 
         <p><span class="style4"><b>Image Description:</b> <?php echo htmlentities($xmlFile->getAttribute('description'));?> <br /> 
          <b>Image contained in folder:</b> <?php echo htmlentities($xmlFile->getAttribute('folder'));?> </span><br /> 
          <?php endfor; ?> 
          </li> 
        </p> 
        </ul> 
       </div> 
       </div> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
    </div> 
     <div class="aB-a">  </div> 
     </div> 
    </div> 
    </div> 
    </form> 
</body> 
</html> 

此頁面的數據來自文件'files.xml',其中的摘錄可以在下面找到。

<?xml version="1.0" encoding="utf-8" ?> 
- <files> 
    <file name="Test 1/article-0-07D01B74000005DC-138_468x617.jpg" source="article-0-07D01B74000005DC-138_468x617.jpg" size="143110" originalname="article-0-07D01B74000005DC-138_468x617.jpg" thumbnail="article-0-07D01B74000005DC-138_468x617.jpg" description="No description provided" userid="1" locationid="1" folder="Test_1" /> 
    </files> 

我想要做的是過濾返回的記錄,是由「用戶ID」和「locationid」和用於測試目的更具體的這些有「1」的值。

我從XML 'Select Where' Statement這個網站獲得了一些有用的指導,並且從我讀過的後續'XPath'教程中,我將我認爲是正確的xpath代碼放在一起,這是我的頂部'畫廊'頁腳本。

然而,當我試圖運行此我收到以下錯誤:

Parse error: syntax error, unexpected '=' in /homepages/2/d333603417/htdocs/development/gallery.php on line 16

很顯然,這個問題是與「等於」,但是當我又回到了教程,這似乎是正確的語法使用。

我只是想知道是否有人可以看看這個請讓我知道我哪裏出了問題。

非常感謝

修訂後的XPath代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<?php 

    $galleryPath = 'UploadedFiles/'; 

    $thumbnailsPath = $galleryPath . 'Thumbnails/'; 

    $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 

    $descriptions = new DOMDocument('1.0'); 
    $descriptions->load($absGalleryPath . 'files.xml'); 

    /files/file[@userid="1" and @locationid="1"]; 


?> 

回答

1

16號線是

path="files[userid=1]/originalname/folder/description/source/thumbnail"; 

如果這是PHP代碼,你需要$path之前。

該行下方的代碼似乎是Javascript。所以,你應該通過PHP變量$路徑進入的Javascript語法,如:

var path = "<?=$path ?>"; 

HTH,

+0

你好,非常感謝這一點。我必須承認,在這裏我的深度已經相當大,因爲除了我已經有的腳本之外,我不確定我需要包括什麼。我最初包含的代碼來自我一直在努力的一個教程。我剛剛嘗試使用'$ path ='文件[userid = 1]/originalname/folder/description/source/thumbnail';'by itslef但不幸的是它仍然檢索所有的記錄。親切的問候 – IRHM 2012-03-17 18:13:36

+0

不清楚你是如何使查詢「文件[userid = 1] /原始名稱/文件夾/說明/源/縮略圖」。 – 2012-03-17 23:08:09

+0

從XML中,它看起來像「原始名稱」,「文件夾」,「描述」等是「文件」的屬性,在這種情況下,查詢可能應該像「/ files/file [@ userid =」1「 ]「只是用於userid 1.如果你想通過更多的屬性進行搜索,然後」/文件/文件[@ userid =「1」和@ originalname =「somename」]「等等 – 2012-03-17 23:16:49