2012-03-19 65 views
1

我想知道是否有人可以帮助我。使用Xpath来筛选记录

我使用下面的脚本来从一个XML文件中的图像加载到一个网页。

<!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. 
    //Do not forget about the slash in the end of the folder name. 
    $galleryPath = 'UploadedFiles/'; 

    $thumbnailsPath = $galleryPath . 'Thumbnails/'; 

    $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 

    $descriptions = new DOMDocument('1.0'); 
    $descriptions->load($absGalleryPath . 'files.xml'); 
?> 
<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;?> 
      <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> 

修正代码在XPath - NB不是工作方案

<!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. 
    //Do not forget about the slash in the end of the folder name. 
    $galleryPath = 'UploadedFiles/'; 

    $thumbnailsPath = $galleryPath . 'Thumbnails/'; 

    $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 

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

    $xpath = new DOMXPATH($descriptions); 
?> 
<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;?> 
      <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++) : 
          $name = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@originalname"); 
          $description = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@description"); 
          $folder = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@folder"); 
          $source = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@source");   
          $thumbnail = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@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 $description;?> <br /> 
          <b>Image contained in folder:</b> <?php echo $folder;?> </span><br /> 

          </li> 
         </p> 
      <?php endfor; ?> 
</ul> 
       </div> 
       </div> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
    </div> 
     <div class="aB-a">  </div> 
     </div> 
    </div> 
    </div> 
    </form> 
</body> 
</html> 

,这是一个被加载到页面的XML文件:

<?xml version="1.0" encoding="utf-8" ?> 
- <files> 
    <file name="Test 1/_47061196_greatbritainjpg.jpg" source="_47061196_greatbritainjpg.jpg" size="227505" originalname="_47061196_greatbritainjpg.jpg" thumbnail="_47061196_greatbritainjpg.jpg" description="No description provided" userid="1" locationid="1" folder="Test_1" /> 
    <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" /> 
    <file name="Test 1/stags-snow_1544533c.jpg" source="stags-snow_1544533c.jpg" size="21341" originalname="stags-snow_1544533c.jpg" thumbnail="stags-snow_1544533c.jpg" description="No description provided" userid="1" locationid="1" folder="Test_1" /> 
    </files> 

我想要做的是使图像加载用户特定的,所以例如在'locationid'值为'1'的值'1'的'userid'只能查看具有匹配值的图像在t他是XML文件。

我知道,从我读过的“Xpath的”语句是能够做到这一点的教程和文章。但是,似乎有很多不同的方式来做到这一点,并且XML非常非常少,我真的不知道从哪里开始。

我只是想知道是否有人也许可以给我一些帮助,请帮助我能够做到这一点。

亲切的问候

回答

1

没问题!

为了您的XPath语句,我假设你想回到哪个XML节点包含用户名和locationid您需要的文件名。

所以,你会先启动根节点,然后检查该文件中有这样的标准:

locationid和用户ID是两个属性,所以你需要的XPath引用时,他们使用的@符号。

$xpath = new DOMXPATH($descriptions) ; 
$result = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@name"); 

这可能看起来有点混乱,因为你的属性名相同,输入名称,但是在变量“”指的是该块:

<input name="userid" type="text" id="userid" value="1" /> 
<input name="locationid" type="text" id="locationid" value="1" /> 

所以这会给你该用户的文件的名称,你可以应用到你的$源标签。只需重复给定的xpath并附加/ @描述或/ @缩略图代替名称即可获得其他值。

希望这可以帮助,让我知道如果我需要澄清任何事情!

试试这个:

(不知道在你的输入形式的原代码去了,但如果你还在使用它:)

<ul class="gallery-image-list"> 
        <?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) : 
          $name = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@originalname"); 
          $description = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@description"); 
          $folder = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@folder"); 
          $source = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@source");   
          $thumbnail = $xpath->query("files/file[@userid=" . userid . " and @locationid=" . locationid . "]/@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 $description;?> <br /> 
          <b>Image contained in folder:</b> <?php echo $folder;?> </span><br /> 

          </li> 
         </p> 
      <?php endfor; ?> 
</ul> 
+0

你好,非常感谢你的回复到我的帖子。我在'$ descriptions-> load($ absGalleryPath。'files.xml');'下面的脚本的顶部添加了这行代码,'但是你能告诉我吗?我认为有一个“在Dreamweaver中似乎并没有关闭这行代码 – IRHM 2012-03-20 15:20:55

+0

啊,是的,忘记了结束标记,很好的抓住了!对不起,我修改了我的帖子。 – JWiley 2012-03-20 15:31:05

+0

嗨,非常感谢您的确认,这是一个诚恳的猜测:)我很抱歉与此有关的痛苦,但我已经添加了行'$ result = $ xpath-> query(“文件/文件[@ userid =“。userid。”和@ locationid =“。locationid。”]/@ name“); '在'$ descriptions-> load($ absGalleryPath。'files.xml')的第16行;'但是我收到以下错误:'致命错误:调用成员函数对/ homepages中的非对象的query()在线16上的/2/d333603417/htdocs/development/gallery.php。也许可以解释我做错了吗? – IRHM 2012-03-20 15:34:34