0

I want to select elements which have a certain attribute value. This is an example of my xml code :

    <response>
<result name="response" numFound="1171" start="0" maxScore="1.0">
<doc>
<float name="score">1.0</float>
<arr name="bm_field_candidat_valid_indexing">
<bool>false</bool>
</arr>
<str name="content">
Etudiant, jeune diplômé Informatique, nouvelles technologies Bac+5 
</str>
<arr name="sm_field_candidat_telephone">
<str>0336548651</str>
</arr>
<arr name="sm_field_candidat_ville">
<str>madrid</str>
</arr>
<arr name="sm_field_profil_candidat_compete">
<str>
Logiciel(s) bureautique: Base de données (type Access...), Présentation (type Powerpoint...), Tableur (type Excel...), Traitement de texte (type Word...) | Langage(s) de programmation: C/C++, Delphi(5/6), VB(5/6), C++builder, Java, SQL, Merise, ASP | Logiciel(s): Dreamweaver
</str>
</arr>
<arr name="sm_vid_Career_levels">
<str>Etudiant, jeune diplômé</str>
</arr>
<arr name="sm_vid_Countries">
<str>Maroc</str>
<str>Liste non ordonnée</str>
</arr>
<arr name="sm_vid_Education_levels">
<str>Bac+5 et plus</str>
</arr>
<arr name="sm_vid_Languages">
<str>maternelle</str>
</arr>
<str name="ss_name_formatted">evinalio</str>
<str name="teaser">
Etudiant, jeune diplômé americaine Mr fant rue de Libourne  » Informatique, nouvelles technologies  
</str>
<arr name="tm_vid_13_names">
<str>maternelle arabe</str>
</arr>
</doc>
</result>
</response>

I want to get the values that are defined by attributes name, this is my code :

 $url = "http://192.168.164.237:nOther=&hl.fl=";
  $xml = simplexml_load_file($url);
  $string = $xml->asXML();
  $res = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
  $res = $res->xpath('//doc');

  $dom = new DOMDocument();
  $dom->preserveWhiteSpace = false;
  $dom->loadXml($res);
  $dom->saveXML();

  $xPath = new DOMXPath($dom); 
  $storeNodes = $xPath->query( '//arr[@name="sm_field_profil_candidat_compete"]');
  print_r($storeNodes);

And this is what I got:

DOMNodeList Object ( [length] => 0 )

EzLo
  • 13,780
  • 10
  • 33
  • 38
a.hitos
  • 3
  • 2
  • 3
    Possible duplicate of [XPath to select Element by attribute value](https://stackoverflow.com/questions/14248063/xpath-to-select-element-by-attribute-value) – Cleptus Jun 26 '18 at 11:14

1 Answers1

1

You can not load an array of SimpleXMLElement as XML string. Use dom_import_simplexml()

$docElement = $res->xpath('//doc')[0];
$docNode = dom_import_simplexml($docNode);
$domDocument = $docNode->ownerDocument();
$xpath = new DOMXpath($domDocument);

However here is no need to load the response into SimpleXML at all:

$xml = file_get_contents($url);
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
$document->loadXml($xml);
$xpath = new DOMXpath($document); 

$storeNodes = $xpath->query(
  '//doc//arr[@name="sm_field_profil_candidat_compete"]'
);
var_dump($storeNodes);

Output:

object(DOMNodeList)#4 (1) {
  ["length"]=>
  int(1)
}

If you have multiple doc elements, you can fetch them first and use them as a context for a second expression:

foreach ($xpath->query('//doc') as $docNode) {
  $storeNodes = $xpath->query(
    './/arr[@name="sm_field_profil_candidat_compete"]', $docNode
  );
  var_dump($storeNodes);
}
ThW
  • 19,120
  • 3
  • 22
  • 44
  • If I had a several doc node the variable $storesNodes will contain all data elements of all docs but in my case I want to iterate on each doc apart so i can treat each element apart. How can i do in this case ? – a.hitos Jun 26 '18 at 13:50
  • Added an example for that. – ThW Jun 26 '18 at 16:01