loo9162 Posted April 10, 2013 Share Posted April 10, 2013 I'm trying to pick out items from an XML file by their titles using Xpath in DOM, but because some have apostrophes, and others have quotes, it doesn't work. I've tried replacing the apostrophes using this $query1 = 'channel/item[title='.$p.']/title'; but it only works for the apostrophes, not the quotes. Any advice on how to do this? <?php $q = $_GET["q"];$q = explode('|^', $q);$counts = count($q);unset($q[$counts-1]);$p = stripslashes($q);$q = stripslashes($p);$dom = new DOMDocument;$dom->preserveWhiteSpace = false;$dom->formatOutput = true;$dom->Load("../$userid.xml");$xpath = new DOMXPath($dom);foreach ($q as $r) {$p = preg_replace("/'/","",$r); $query1 = "channel/item[title="'.$p.']/title";$query2 = "channel/item[title="'.$p.']/url";$query3 = "channel/item[title="'.$p.']";$entries = $xpath->query($query1);$entries2 = $xpath->query($query2);$entries3 = $xpath->query($query3);foreach ($entries as $entry) {foreach ($entries2 as $entry2) {foreach ($entries3 as $entry3) {$oldchapter = $entry->parentNode->removeChild($entry);$oldchapter2 = $entry2->parentNode->removeChild($entry2);$oldchapter3 = $entry3->parentNode->removeChild($entry3);$dom->preserveWhiteSpace = false;}}}}$dom->formatOutput = true;$dom->save("../$userid.xml") ?> Basically, my code extracts titles from a URL, separated by "|^" (For example title1|^title2|^title3|^). Because the "|^" is appended to the end of each title, I have to remove the empty value from the array. Then, because the titles with apostrophes come in like "Title\\'s", I have to strip slashes twice to get rid of them. Then I load a new DOMdocument, and find the titles from the URL in my existing XML document. Then I want the code to remove the whole items (titles, urls and the item itself) which have the same titles as the ones in the URL, and then save the document. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 10, 2013 Share Posted April 10, 2013 (edited) I believe you have to do some ugly concat() stuff. Given a string like Colin "The Hand" O'Malley you need to end up with concat("Colin ", '"', "The Hand", '"', " O'Malley")and the query channel/item[title=concat("Colin ", '"', "The Hand", '"', " O'Malley")]/titleHacking something out, function escapexpath($string) { $return = array(); foreach (explode('"', $string) as $part) { $return && $return[] = "'\"'"; $return[] = '"' . $part . '"'; } if (count($return) > 1) { return "concat(" . implode(",", $return) . ")"; } else { return '"' . $string . '"'; } } Edited April 10, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
loo9162 Posted April 11, 2013 Author Share Posted April 11, 2013 I believe you have to do some ugly concat() stuff. Given a string like Colin "The Hand" O'Malley you need to end up with concat("Colin ", '"', "The Hand", '"', " O'Malley")and the query channel/item[title=concat("Colin ", '"', "The Hand", '"', " O'Malley")]/titleHacking something out, function escapexpath($string) { $return = array(); foreach (explode('"', $string) as $part) { $return && $return[] = "'\"'"; $return[] = '"' . $part . '"'; } if (count($return) > 1) { return "concat(" . implode(",", $return) . ")"; } else { return '"' . $string . '"'; } } I'm sorry, but I'm a bit confused, I don't understand what the code is supposed to do Quote Link to comment Share on other sites More sharing options...
requinix Posted April 11, 2013 Share Posted April 11, 2013 It returns a concat() thing or a regular "" string depending on what quotes are being used. $query1 = "channel/item[title=" . escapexpath($p) . "]/title"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.