Jump to content

Recommended Posts

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.

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")]/title
Hacking 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 by requinix

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")]/title
Hacking 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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.