note 127677 added to simplexmlelement.xpath

From: Date: Wed, 28 Sep 2022 09:56:33 +0000
Subject: note 127677 added to simplexmlelement.xpath
Groups: php.notes 
Request: Send a blank email to php-notes+get-219625@lists.php.net to get a copy of this message
You can also search for siblings with a condition. For example immagine to need the title for a desidered language which with the two siblings tags in this XML file <?xml version="1.0" encoding="UTF-8"?> <IDOC BEGIN="1"> ... <PRODUCT SEGMENT="1"> <PRODUCTCODE>005</PRODUCTCODE> <LANG>E</LANG> <TITLE>Name</TITLE> <LANG_ISO>EN</LANG_ISO> </PRODUCT> <PRODUCT SEGMENT="1"> <PRODUCTCODE>005</PRODUCTCODE> <LANG>I</LANG> <TITLE>Name I</TITLE> <LANG_ISO>IT</LANG_ISO> </PRODUCT> ... </IDOC> Let's break it down <?php "//PRODUCT" // find product tag "//PRODUCT/LANG[.='E']" // whithin it find lang == "E" "//PRODUCT/LANG[.='E']/../" // go up one step "//PRODUCT/LANG[.='E']/../TITLE" // get the title Tag content // Combined search and find siblings ---------------------------- $xml = simplexml_load_file("fname.xml"); // Search for the TITLE field which sibling is == "E" ---------- $title_E_array = $xml->xpath("//PRODUCT/LANG[.='E']/../TITLE"); // By default gives back an array of SimpleXmlElement $title = (string) $xml->xpath("//PRODUCT/LANG[.='E']/../TITLE")[0]; // in this way saves only the value as a string ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 89.190.184.61) ---- Manual Page -- https://php.net/manual/en/simplexmlelement.xpath.php Edit -- https://main.php.net/note/edit/127677 Del: integrated -- https://main.php.net/note/delete/127677/integrated Del: useless -- https://main.php.net/note/delete/127677/useless Del: bad code -- https://main.php.net/note/delete/127677/bad+code Del: spam -- https://main.php.net/note/delete/127677/spam Del: non-english -- https://main.php.net/note/delete/127677/non-english Del: in docs -- https://main.php.net/note/delete/127677/in+docs Del: other reasons-- https://main.php.net/note/delete/127677 Reject -- https://main.php.net/note/reject/127677 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219625) next »