2019-06-11 17:26
浏览 176


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!

图片转代码服务由CSDN问答提供 功能建议


   &lt;?xml version =“1.0”encoding =“UTF-8”?&gt; 
&lt; facilities&gt; 
&lt; areas&gt; 
&lt; area name =“Rocket”&gt; 
&lt; trails&gt; 
  &lt; trail name =“This Skiway”status =“CLOSED”difficulty =“novice”/&gt; 
&lt; / trails&gt; 
&lt; / area&gt; 
&lt; / areas&gt; 
&lt; / facilities&gt; \  n   

我正在尝试让属性显示在表单中,以便我可以替换它们。 我已经成功使用了getElementsbyTagName并替换了标签内的内容,但是当我引入XPath并尝试替换属性时,它只是不起作用。

我正在使用的代码是 这个:

 &lt; script src =“”&gt;&lt; / script&gt; 
&lt;  ;?php 
 $ xml = new DOMDocument('1.0','utf-8'); 
 $ xml-&gt; formatOutput = true;  
 $ xml-&gt; preserveWhiteSpace = false; 
 $ xml-&gt; load('examples.xml'); 
 $ xpath = new DOMXpath($ xml); 
 $ name = $ xpath  - &gt;查询(“/ facilities / areas / area [@ name ='Rocket'] / trails / trail [@ name ='This Skiway'] / @ name”) - &gt; item(0); 
 $ status  = $ xpath-&gt; query(“/ facilities / areas / area [@ name ='Rocket'] / trails / trail [@ name ='This Skiway'] / @ status”) - &gt; item(0); \  n 
 $ xpath-&gt; replaceChild($ name,$ name); 
 $ xpath-&gt; replaceChild($ status,$ status); 
 if(  isset($ _ POST ['submit']))
 $ name-&gt; nodeValue = $ _POST ['namanya']; 
 $ status-&gt; nodeValue = $ _POST ['statusnya']; 
htmlentities  ($ xml-&gt; save('examples.xml')); 
&lt; form method =“POST”action =''&gt; 
 name&lt;  input type =“text-name”value =“&lt;?php echo $ name-&gt; nodeValue?&gt;”  name =“namanya”/&gt; 
&lt; span&gt;&lt; label for ='statusnya'&gt; status&lt; / label&gt; 
&lt; select name =“statusnya”id =“statusnya”&gt; 
&lt; 选项选择值=“&lt;?php echo $ status-&gt; nodeValue?&gt;”&gt;&lt;?php echo $ status-&gt; nodeValue?&gt;&lt; / option&gt; 
&lt; option value =“OPEN”  &gt; OPEN&lt; / option&gt; 
&lt; option value =“CLOSED”&gt; CLOSED&lt; / option&gt; 
&lt; option value =“RACING CLOSURE”&gt; RACING CLOSURE&lt; / option&gt; 
&lt; / select&gt;&lt; /  span&gt; 
&lt; input name =“submit”type =“submit”/&gt; 
&lt; / form&gt; 

不确定我是什么 在这里做错了但我认为我的xpath查询不知何故。 非常感谢!

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

1条回答 默认 最新

  • 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);
    打赏 评论

相关推荐 更多相似问题