2019-06-11 17:26
浏览 153


I have an XML file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
        <area name="Rocket">
                <trail name="This Skiway" status="CLOSED" difficulty="novice"/>

I'm trying to make the attributes show up in a form so that I can replace them. I've had success with getElementsbyTagName and replacing content inside a tag, but when I introduce XPath and try to replace the attributes it just doesn't work.

The code I'm using is this:

<script src=""></script>
 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;

 $xpath = new DOMXpath($xml);

 $name = $xpath->query("/facilities/areas/area[@name='Rocket']/trails/trail[@name='This Skiway']/@name")->item(0);
 $status = $xpath->query("/facilities/areas/area[@name='Rocket']/trails/trail[@name='This Skiway']/@status")->item(0);

 $xpath->replaceChild($name, $name);
 $xpath->replaceChild($status, $status);

 if (isset($_POST['submit']))
$name->nodeValue = $_POST['namanya'];
$status->nodeValue = $_POST['statusnya'];



<form method="POST" action=''>
  name <input type="text-name" value="<?php echo $name->nodeValue  ?>" name="namanya" />

<span><label for='statusnya'>status </label>
<select name="statusnya" id="statusnya">
<option selected value="<?php echo $status->nodeValue  ?>"><?php echo $status->nodeValue  ?></option>
<option value="OPEN">OPEN</option>
<option value="CLOSED">CLOSED</option>
<option value="RACING CLOSURE">RACING CLOSURE</option>

<input name="submit" type="submit" />

Not sure what I'm doing wrong here but I think my xpath query is bad somehow. Many thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dragon8899
    dragon8899 2019-06-11 20:16

    The answer as Nigel Ren suggested was just to remove these two lines, as they no longer apply:

     $xpath->replaceChild($name, $name);
     $xpath->replaceChild($status, $status);
    点赞 评论