note 127676 added to simplexmlelement.xpath

From: Date: Wed, 28 Sep 2022 07:02:18 +0000
Subject: note 127676 added to simplexmlelement.xpath
Groups: php.notes 
Request: Send a blank email to php-notes+get-219624@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 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> For clarity this is how it works //PRODUCT/ -> find product tag LANG[.='E'] -> whithin it find lang == "E" LANG[.='E']../ -> go up one step LANG[.='E']../TITLE -> get the title Tag content <?php // Combined search and find siblings ---------------------------- $xml = simplexml_load_file("fname.xml"); // Search for the TITLE field which sibling is == "E" ---------- $xmlAssociation->xpath("//PRODUCT/LANG[.='E']/../TITLE"); ?> ---- 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/127676 Del: integrated -- https://main.php.net/note/delete/127676/integrated Del: useless -- https://main.php.net/note/delete/127676/useless Del: bad code -- https://main.php.net/note/delete/127676/bad+code Del: spam -- https://main.php.net/note/delete/127676/spam Del: non-english -- https://main.php.net/note/delete/127676/non-english Del: in docs -- https://main.php.net/note/delete/127676/in+docs Del: other reasons-- https://main.php.net/note/delete/127676 Reject -- https://main.php.net/note/reject/127676 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219624) next »