简体   繁体   中英

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. 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").

The XML is like that :

<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

 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 :/ ). 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?

Here is the 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. But I cannot filter it with the "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! To sum up : please help! Thanks!

It seems that you want to fetch the BIEN element nodes that fulfill a specific condition. You should do this from the start and not fetch the MAISON or APPARTEMENT element nodes:

$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
  • Fetch any BIEN element node...
    //BIEN
  • ...that has a MAISON with and SS_TYPE equal to 2 ...
    //BIEN[MAISON/SS_TYPE=2]
  • ...and has a NEGOCIATEUR with and NEGOCIATEUR_ID equal to 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! 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 :| ...

WIth your solution, my get_parent_node function is useless and the HTML looks like that :

  <?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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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