XPATH problem

Discussion in 'PHP' started by Teessider_2000, Jan 12, 2009.

  1. #1
    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
     
    Teessider_2000, Jan 12, 2009 IP
  2. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    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?
     
    yoavmatchulsky, Jan 12, 2009 IP
  3. Teessider_2000

    Teessider_2000 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    Teessider_2000, Jan 12, 2009 IP
  4. THFS

    THFS Peon

    Messages:
    80
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Is the problem solved? If not try adding double ccs hash at the end.
     
    THFS, Jan 12, 2009 IP
  5. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    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:
     
    yoavmatchulsky, Jan 12, 2009 IP
  6. Teessider_2000

    Teessider_2000 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Fantastic! You have saved the day.

    You rock man!
     
    Teessider_2000, Jan 13, 2009 IP