dousou3027 2016-03-05 09:58
浏览 113
已采纳

如何通过PHP(domx)中包含冒号的属性名称选择xml元素?

Similar to the question here, I am trying to select xml elements by their attribute name, which contains a colon.

I am trying to do this with PHP, and my final goal is, to replace the content of these elements.

My code does not return any result:

$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<a>
<aff xml:id="af1" value="a">
    <uAff>
        Hello
    </uAff>
</aff>
<aff xml:id="corr1">
    <uAff>
        Hello1
    </uAff>
</aff>
</a>';

$dom = new DOMDocument;
$dom->loadHTML($xml);
$xpath = new DOMXPath($dom);

$xelems = $xpath->query("/a/aff/@*[name()='xml:id']");
print_r($xelems);

foreach ($xelems as $node) {
    $node->nodeValue = 'New attribute value'; // changes the value of the xml:id attribute.';
   $node->parentNode->nodeValue = 'This does work!'; // changes the value of the element
}
echo htmlentities($dom->saveHTML());

How can this work with PHP?

  • 写回答

1条回答 默认 最新

  • dongzouche9108 2016-03-05 10:04
    关注

    The input is XML, so use loadXML instead of loadHTML.

    * selects elements. @* selects attributes.

    @*[name()='xml:id']
    

    You might also need to start the expression by // to make it search everywhere, not just under the root node.

    But then, much simpler expression might be used:

    //@xml:id
    

    or, with the full path

    /a/aff/@xml:id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog