dongya767979565 2018-02-28 15:30
浏览 85
已采纳

xpath搜索属性时出错

Hello I would like to extract with xpath all items with the name="Frequency" which contains a certain value but it doesn't give me anything. What Am I doing wrong?

XML File:

<products>
    <product>
        <title>PT2400</title>
        <ElectricSpecifications>
           <item name="Frequency">2310 - 2485 MHz (WLAN, BLUETOOTH, ZIGBEE</item>
        </ElectricSpecifications>
    </product>
</products>


foreach ($xpath->query("//product/ElectricSpecifications[@item='Frequency'][contains(., ' $value')]") as $item) {
    var_dump($item);
}
  • 写回答

1条回答 默认 最新

  • douwen3127 2018-02-28 15:36
    关注

    item is not an attribute of ElectricSpecifications, but its child node, so instead of

    [@item='Frequency']
    

    syntax, try

    item[@name='Frequency']
    

    And so complete XPath should be like

    //product/ElectricSpecifications/item[@name='Frequency' and contains(., ' $value')]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?