简体   繁体   English

如何使用Xpath过滤XML导出的搜索?

[英]How can I filter a search on a XML export with Xpath?

I'm currently working on a real estate website. 我目前在房地产网站上工作。 The data of the properties are retrieved from an XML export via an URL. 通过URL从XML导出中检索属性的数据。 There are properties to sell and rentals, but in two different geographical locations. 有出售和出租的物业,但位于两个不同的地理位置。 What I would like to do is two pages each corresponding to a location and displaying houses and appartments from the location, but without rentals. 我想做的是两个页面,每个页面对应一个位置并显示该位置的房屋和公寓,但没有租金。 The location is set by a broker ID (here "NEGOCIATEUR_ID"). 该位置由经纪人ID(此处为“ NEGOCIATEUR_ID”)设置。

The XML is like that : XML是这样的:

<BIEN>
 <INFO_GENERALES>...</INFO_GENERALES>
 <AGENCE>...</AGENCE>
 <NEGOCIATEUR>
   <NEGOCIATEUR_ID>452329</NEGOCIATEUR_ID>
 </NEGOCIATEUR>
 <VENTE>...</VENTE>
 <MAISON>...</MAISON>
 <LOCALISATION>...</LOCALISATION>
</BIEN>

Here's my current PHP code : 这是我当前的PHP代码:

<?php

 class noeud_parent extends SimpleXMLElement
 {
   public function get_parent_node()
   {
    // return current($this->xpath('//BIEN/* | //BIEN/NEGOCIATEUR[NEGOCIATEUR_ID=436344] | not //BIEN/LOCATION'));
    return current($this->xpath('parent::*'));
   }
  }

  $url = 'http://clients.ac3-distribution.com/office5/ageprim/cache/export.xml';
  $xml = simplexml_load_file($url, 'noeud_parent');

  $maisons = $xml->xpath('//BIEN/MAISON[SS_TYPE=2]');
  $appartements = $xml->xpath('//BIEN/APPARTEMENT[SS_TYPE=1]');

 ?>

The line in comments in the public function is the result that I want (of course it is not working like that :/ ). public function中的注释行是我想要的结果(当然,它不是这样工作的:/)。 AND I did'nt even exclude the rentals (if it's a rental, is replaced by <LOCATION> )... Is it even possible to do all in one single line? 而且我什至没有排除租金(如果是租金,则由<LOCATION>代替)...甚至可以在一行中全部完成吗?

Here is the HTML : 这是HTML:

<?php foreach ($maisons as $bien): ?>
  <article class="bien">
    <div class="photo_bien"><img src="<?= $bien->get_parent_node()->IMAGES->IMG[0] ?>"/></div>
    <h4><?= $bien->get_parent_node()->LOCALISATION->VILLE ?></h4>
    <p><?= $bien->get_parent_node()->INTITULE->FR ?></p>
    <p><strong>Nbre de pièces :</strong> <?= $bien->get_parent_node()->MAISON->NBRE_PIECES ?></p>
    <a href="annonce.php?aff_id=<?= $bien->get_parent_node()->INFO_GENERALES->AFF_ID ?>">VOIR PLUS</a>
  </article>
<?php endforeach ?>

The get_parent_node function helps me to go backward to get the infos from the same level nodes. get_parent_node函数可帮助我向后退以从相同级别的节点获取信息。 But I cannot filter it with the "NEGOCIATEUR_ID"... 但是我无法使用“ NEGOCIATEUR_ID”对其进行过滤...

Sorry if I sort of spammed my post with so much code, but my PHP skills are basic and I'm stuck up on this problem! 抱歉,如果我向我的帖子发送了这么多代码,但我的PHP技能是基础知识,我就此问题为难! To sum up : please help! 总结一下:请帮忙! Thanks! 谢谢!

It seems that you want to fetch the BIEN element nodes that fulfill a specific condition. 看来您想获取满足特定条件的BIEN元素节点。 You should do this from the start and not fetch the MAISON or APPARTEMENT element nodes: 您应该从头开始,不要获取MAISONAPPARTEMENT元素节点:

$xml = <<<'XML'
<BIEN>
 <INFO_GENERALES>...</INFO_GENERALES>
 <AGENCE>...</AGENCE>
 <NEGOCIATEUR>
   <NEGOCIATEUR_ID>452329</NEGOCIATEUR_ID>
 </NEGOCIATEUR>
 <VENTE>...</VENTE>
 <MAISON>
   <SS_TYPE>2</SS_TYPE>
 </MAISON>
 <LOCALISATION>...</LOCALISATION>
</BIEN>
XML;

$xml = simplexml_load_string($xml);
foreach ($xml->xpath('//BIEN[MAISON/SS_TYPE=2 and NEGOCIATEUR/NEGOCIATEUR_ID = 452329]') as $bien) {
  var_dump(
      (string)$bien->NEGOCIATEUR->NEGOCIATEUR_ID
  );
}

Output: 输出:

string(6) "452329"
Xpath Expression Xpath表达式
  • Fetch any BIEN element node... 获取任何BIEN元素节点...
    //BIEN
  • ...that has a MAISON with and SS_TYPE equal to 2 ... ...的MAISONSS_TYPE等于2 ...
    //BIEN[MAISON/SS_TYPE=2]
  • ...and has a NEGOCIATEUR with and NEGOCIATEUR_ID equal to 452329 ... ...并且具有NEGOCIATEURNEGOCIATEUR_ID等于452329 ...
    //BIEN[MAISON/SS_TYPE=2 and NEGOCIATEUR/NEGOCIATEUR_ID = 452329]

Yes it works!! 是的,它有效!! I had tried this type of instruction with 'and' but I was wrong with the syntax! 我曾尝试使用'and'进行此类指令,但语法错误! it goes like this : 它是这样的:

$maisons = $xml->xpath('//BIEN[MAISON/SS_TYPE=2 and NEGOCIATEUR/NEGOCIATEUR_ID = 452329]');
$appartements = $xml->xpath('//BIEN[APPARTEMENT/SS_TYPE=1 and NEGOCIATEUR/NEGOCIATEUR_ID = 452329]');

I was trying to find my answer with xquery and Zorba but it would have been sooo much complicated considering my problem :| 我试图用xquery和Zorba找到我的答案,但是考虑到我的问题,它会变得非常复杂:| ... ...

WIth your solution, my get_parent_node function is useless and the HTML looks like that : 通过您的解决方案,我的get_parent_node函数没有用,HTML看起来像这样:

  <?php foreach ($maisons as $bien): ?>
<article class="bien">
  <div class="photo_bien"><img src="<?= $bien->IMAGES->IMG[0] ?>"/></div>
  <h4><?= $bien->LOCALISATION->VILLE ?></h4>
  <p><strong>Prix :</strong> <?= $bien->VENTE->PRIX ?> euros</p>
  <p><strong>Agence :</strong> <?= $bien->AGENCE->AGENCE_VILLE ?></p>
  <a href="annonce.php?aff_id=<?= $bien->INFO_GENERALES->AFF_ID ?>">VOIR PLUS</a>
</article>

So let me say a big THANK YOU! 所以,我要非常感谢您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM