在PHP中使用XPath替换XML属性

我有一个如下所示的XML文件:</ p>

   &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 </ code> </ pre>

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

我正在使用的代码是 这个:</ p>

 &lt; script src =“http://code.jquery.com/jquery-latest.min.js”&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);
?&gt;

&lt;?php
if( isset($ _ POST ['submit']))
{
$ name-&gt; nodeValue = $ _POST ['namanya'];
$ status-&gt; nodeValue = $ _POST ['statusnya'];
htmlentities ($ xml-&gt; save('examples.xml'));

}

?&gt;

&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;
</ code> </ pre>

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

展开原文

原文

I have an XML file that looks like this:

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

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="http://code.jquery.com/jquery-latest.min.js"></script>
 <?php
 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;
 $xml->load('examples.xml');

 $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);
 ?>

 <?php
 if (isset($_POST['submit']))
 {
$name->nodeValue = $_POST['namanya'];
$status->nodeValue = $_POST['statusnya'];
htmlentities($xml->save('examples.xml'));

 }

 ?>

<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>
</select></span>

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

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

douxian3170
douxian3170 尝试只记录这两行,因为数据更新是在对nodeValue的赋值中完成的。
一年多之前 回复
douyi8760
douyi8760 当我使用$name=$element->getElementsByTagName('name')->item(0);replaceChild是我以前做出的改变,它起作用了。我是PHP的新手。如果它不能与XPath一起使用,我会用什么呢?
一年多之前 回复
douhuibo5635
douhuibo5635 您的XML格式错误(缺少一些结束标记-路径和区域)。还不确定你要对replaceChild()的两次调用做什么,因为这个方法不存在。
一年多之前 回复

1个回答



Nigel Ren建议的答案就是删除这两行,因为它们不再适用:</ p>

  $ xpath-&gt; replaceChild($ name,$ name); 
$ xpath-&gt; replaceChild($ status,$ status);
</ code> </ pre>
</ DIV>

展开原文

原文

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);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问