【问题标题】:XPath: Select element which has specific attribute with special characterXPath:选择具有特殊字符的特定属性的元素
【发布时间】:2013-10-29 10:51:24
【问题描述】:
<document>
    <element>
        <attribut a:name="my-name">My Name</attribut>
        <attribut a:parent="parent1">Parent 1</attribut>
    </element>
</document>

在这个XML文档中,如何选择属性为a:name的节点?

【问题讨论】:

  • 在这里搜索关于 xpath 和命名空间的问题的答案。
  • 我试试这个,但什么都没选:codepad.org/4V9kUNQM

标签: xpath xml-parsing xml-namespaces


【解决方案1】:
$xmlTest = <<<XML
<?xml version="1.0" encoding="UTF-8" ?>
<document xmlns:a="http://example.org/a">
<element>
    <attribut a:name="my-name">My Name</attribut>
    <attribut a:parent="parent1">Parent 1</attribut>
</element>
</document>
XML;

$xml = new SimpleXMLElement($xmlTest);
echo current($xml->xpath('//element/attribut[@a:name]'));

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2011-10-13
    • 2010-11-02
    • 2011-08-03
    相关资源
    最近更新 更多