Hi, I have an xml file which holds the information for pages in a cms. I want to allow users to update meta descriptions on a page by page basis, someone on here told me about using xpath. Unfortunately i haven't been able to get it to work. The xml is simple and looks like this: <pages> <page id="index"> <filename>index</filename> <pagename>Welcome</pagename> <pagearea>home</pagearea> <template>homepage</template> <keywords/> <description/> <order>2</order> </page> </pages> Can anyone see why this might not work? $doc = new DomDocument; $doc->preserveWhiteSpace = false; $myFile = "../preview/nav.xml"; $doc->load($myFile); $xpath = new domxpath($doc); $description = $xpath->query("/pages/page[@id='$filelist']/filename"); $description->textContent = $_POST['description']; $doc->save($myFile); Thanks in advance. Cheers, Will
try starting your query string with a double slash instead of a single slash if not, what errors are you getting? what doesn't work? (try debugging) what is the $filelist var?
Thanks for the reply. It doesnt give me any error messages, but it doesnt save the xml either. The variable i pass through is the id of the page, posted from a previous form when picking the page to edit. Im all out of ideas! Cheers, Will
ok, i think i found your problem. the $xpath->query() returns DOMNodeList, not DOMElement, so $description doesn't have a "textContent" property. this worked for me: <?php $doc = new DomDocument(); $doc->preserveWhiteSpace = false; $doc->loadXML(<<<XML <pages> <page id="index"> <filename>index</filename> <pagename>Welcome</pagename> <pagearea>home</pagearea> <template>homepage</template> <keywords/> <description/> <order>2</order> </page> </pages> XML ); $xpath = new DOMXPath($doc); $description = $xpath->query("//pages/page[@id='index']/description"); $description->item(0)->nodeValue = 'new description'; echo $doc->saveXML(); ?> PHP: