Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用Xpath筛选记录_Xml_Xpath - Fatal编程技术网

Xml 使用Xpath筛选记录

Xml 使用Xpath筛选记录,xml,xpath,Xml,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 specif

我不知道是否有人能帮我

我使用以下脚本将图像从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>

画廊
$(函数(){$('a.fancybox').fancybox();});

  • 图像描述:
    文件夹中包含的图像:

    用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>
    
    
    画廊
    $(函数(){$('a.fancybox').fancybox();});
    
    
  • 图像描述:
    文件夹中包含的图像:

    这是加载到页面中的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”只能查看XML文件中具有匹配值的图像

    从我读过的教程和文章中,我知道“Xpath”语句可以做到这一点。然而,似乎有许多不同的方法可以做到这一点,而且由于XML非常、非常、很少,我真的不知道从哪里开始

    我只是想知道是否有人可以提供一些帮助,请帮助我能够做到这一点

    亲切的问候

    没问题

    对于xpath语句,我假设您希望返回包含所需userid和locationid的任意xml节点的文件名

    因此,首先从根节点开始,然后检查哪个文件具有以下条件:

    locationid和userid都是的属性,因此在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" />
    
    
    
    因此,这将为您提供该用户的文件名,您可以将其应用于$source标记。只需重复给定的xpath并附加/@description或/@thumbnail来代替name即可获得其他值

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

    试试这个:

    (不确定输入表单在原始代码中的位置,但如果您仍在使用它:)

    • 图像描述:
      文件夹中包含的图像:

    没问题

    对于xpath语句,我假设您希望返回包含所需userid和locationid的任意xml节点的文件名

    因此,首先从根节点开始,然后检查哪个文件具有以下条件:

    locationid和userid都是的属性,因此在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" />
    
    
    
    因此,这将为您提供该用户的文件名,您可以将其应用于$source标记。只需重复给定的xpath并附加/@description或/@thumbnail来代替name即可获得其他值

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

    试试这个:

    (不确定输入表单在原始代码中的位置,但如果您仍在使用它:)

    • 图像描述:
      文件夹中包含的图像:


    您好,非常感谢您回复我的帖子。我已经在脚本顶部的
    $descriptions->load($absGalleryPath.'files.xml')下添加了这一行,但是你能告诉我,我认为有一个“最后失踪了。在Dreamweaver中,该行似乎没有关闭?好样的,是的,忘了结尾的标记,接得好!很抱歉,我编辑了我的帖子并进行了更正。您好,非常感谢您的确认,说实话这有点像猜测:)我很抱歉对此感到痛苦,但我添加了一行
    $result=$xpath->query(“files/file[@userid=“.userid.”和@locationid=“.locationid.”/@name”)位于
    $descriptions->load($absGalleryPath.'files.xml')下的第16行但我收到以下错误:
    致命错误:在第16行的/homepages/2/d333603417/htdocs/development/gallery.php中对非对象调用成员函数query()。也许能解释一下我做错了什么吗?当然,我主要是想展示如何使用xpath。我将用一些基本设置更新我的答案。同时,这是php/xpath组合的一个很好的链接:我相信错误是因为您放置了$result=行,如下所示