2012-03-19 4 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와 개정 코드 - 워킹 솔루션

<!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 문에 필요한 userid 및 locationid가 포함 된 xml 노드의 파일 이름을 반환한다고 가정합니다. 당신이 XPath는 그들을 참조 할 때 @ 기호를 사용할 필요가 있으므로,

locationid와 사용자 ID의 두 속성은 다음과 같습니다

그래서 먼저이 기준을 가지고있는 파일 확인 후, 루트 노드에서 시작합니다.

$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을 추가하면 다른 값을 얻을 수 있습니다.

도움이 되었으면 좋겠어. 무엇인가 명확하게해야하는지 알려주세요.

이 시도 :

(안 당신은 아직도 그것을 사용하는 경우 :

<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

+0

아, 끝 표지를 잊어 버렸습니다. 죄송합니다. 죄송합니다. 수정 한 게시물을 편집했습니다. – JWiley

+0

안녕하세요, 확인을위한 많은 감사합니다, 그것은 솔직히 추측 조금되었습니다 :)이 고통을 미안 해요,하지만 난'$ 결과 = $ xpath-> 쿼리 (" files/file [@ userid = ". userid."및 @ locationid = ". locationid."]/@ name "); '$ description-> load ($ absGalleryPath. 'files.xml'); '에서 16 행에 있지만 다음과 같은 오류가 발생합니다.'치명적 오류 :/homepages에있는 비 객체의 멤버 함수 query()를 호출하십시오. /2/d333603417/htdocs/development/gallery.php on line 16'을 참조하십시오. 내가 잘못한 것을 설명해 주시겠습니까? – IRHM