AW: xpath getNode problem
Brought to you by:
bs_php,
nigelswinson
|
From: J. C. <jan...@im...> - 2005-08-29 21:55:13
|
Hi
I have used PhpXpath to create an Web-based XML Editor some time ago.
This is some part of it and it might be the code you are looking for.
(especially case "UP" and case "DOWN")
Cheers
Jan
require_once("include/XPath.class.php");
$xpath=new XPath($f_dir.$file."_web.xml");
/*
$xpath=new XPath();
$xpath->setSkipWhiteSpaces();
$xpath->importFromFile($f_dir.$file."_web.xml");
*/
// AKTIONS:
if(isset($_POST["aktion"])){
$path=$_POST["lokation_id"];
$target_path=$_POST["target_path"];
switch($_POST["aktion"]){
case "DEL":
$xpath->removeChild($path,true);
$pos=strrpos($path,"/");
//echo $path;
$path=substr($path,0,$pos);
//echo $path;
break;
case "NEU":
if ($_POST["exelmode"]=="true"){
//Exelmodus:
$namen=split("\r\n",$_POST["atarea"]);
$first=true;
foreach($namen as $name){
if ($name=="") continue;
if($first){
$path_temp=$xpath->appendChild($path,"<data
name=\"".$name."\">".$_POST["text_entry"]."</data>");
$first=false;
}else
$xpath->appendChild($path,"<data
name=\"".$name."\">".$_POST["text_entry"]."</data>");
}
$path=$path_temp;
}else
//normal new entry
$path=$xpath->appendChild($path,"<data
name=\"".$_POST["bezeichnung"]."\">".$_POST["text_entry"]."</data>");
break;
case "EDIT":
$xpath->setAttribute($path,"name",$_POST["bezeichnung"]);
$xpath->replaceData($path,$_POST["text_entry"]);
break;
case "CLONE":
$clonedNode=&$xpath->cloneNode($path,FALSE);
$xpath->insertBefore($path, $clonedNode,true,true);
break;
case "PASTE_INTO":
$clonedNode=&$xpath->cloneNode($target_path,FALSE);
//echo "Clone: ".$path."<br>";
//echo "nach: ".$target_path."<br>";
//$target_path=$xpath->appendData($target_path,"<neu/>");
$path_temp=$xpath->appendChild($path, $clonedNode, FALSE, TRUE); //
insert here
/*
$target_path=$xpath->appendChild($target_path,"<neu/>");
//$path=$xpath->insertChild($target_path, $clonedNode, TRUE, FALSE,
TRUE); // insert here
$xpath->insertBefore($target_path."/neu[1]", $clonedNode, FALSE, TRUE);
// insert here
//$xpath->removeChild($target_path, FALSE); // -> take away
*/
break;
case "UP":
$clonedNode=&$xpath->cloneNode($path,FALSE); // -> duplicate
$xpath->removeChild($path, FALSE); // -> take away
$xpath->insertBefore($target_path, $clonedNode, FALSE, TRUE); // insert
here
$path=$target_path;
break;
case "DOWN":
if ($_POST["toolkit"]=="append"){ // node append to parent node (it is
last element)
$clonedNode=&$xpath->cloneNode($path,FALSE); // -> duplicate
$path_temp=$xpath->appendChild($target_path, $clonedNode, FALSE,
TRUE); // insert here
//echo $path_temp."<br>";
$xpath->removeChild($path, TRUE); // -> take away
$pos1=strrpos($path_temp,"[");
$pos2=strrpos($path_temp,"]")-1;
$pos=substr($path_temp,$pos1+1,strlen($path_temp)-$pos2);
$path=substr($path_temp,0,$pos1)."[".($pos-1)."]";
}else{
$clonedNode=&$xpath->cloneNode($target_path,FALSE); // -> duplicate
//echo "remove:".$path."<br>";
$xpath->removeChild($target_path, FALSE); // -> take away
//echo "insert before:".$path."<br>";
$xpath->insertBefore($path, $clonedNode, FALSE,TRUE); // insert here
$path=$target_path;
}
break;
}
// echo $path; //node to be selected
// die Datei abspeichern...
copy($f_dir.$file."_web.xml",$f_dir."/backup/".time()."_".$file."_web.xml");
$xpath->exportToFile($f_dir.$file."_web.xml",'','<?xml version="1.0"
encoding="iso-88591"'.'?'.'>'); //schreiben
}
-----Ursprüngliche Nachricht-----
Von: php...@li...
[mailto:php...@li...]Im Auftrag von Nigel
Swinson
Gesendet: Montag, 29. August 2005 22:26
An: php...@li...
Cc: Suzanne Slatcher
Betreff: Fw: xpath getNode problem
----- Original Message -----
From: Suzanne Slatcher
To: nig...@us...
Sent: Tuesday, August 23, 2005 7:47 PM
Subject: xpath getNode problem
Hi Nigel
Firstly I should thank you for php.xpath. Without it I don't think I could
have done what I was trying to do.
But there's one thing I can't quite work out -
I have a structure like this to store notes hierarchically:
<NOTES>
<NOTE id="1"><HEAD>heading 1</HEAD><KIDS></KIDS></NOTE>
<NOTE id="2"><HEAD>heading 2</HEAD><KIDS>
<NOTE id="3"><HEAD>heading 3</HEAD><KIDS></KIDS></NOTE>
</KIDS></NOTE>
<NOTE id="4"><HEAD>heading 4</HEAD><KIDS></KIDS></NOTE>
</NOTES>
and what I'd like to do is move 'NOTE 2', along with the contents of it's
KIDS (ie 'NOTE 3') to the KIDS scope of 'NOTE 1'
I'm assuming I'd have to store 'NOTE 2' as a variable, then delete it from
the document, then re-insert it in the new place, but I'm having problems
storing it in a variable.
I've tried various combinations of getNode, eg:
$note[] = $x->getNode('/NOTES[1]/NOTE[2]');
but I must be missing something as print_r($note); is always empty
how would you suggest I go about this?
thankyou very much for your time
Suzanne
--
s p a r k h i v e
hold that thought
www.sparkhive.com
|