首页 文章

在XPath中选择已过滤的节点集的属性

提问于
浏览
0

我正在尝试学习XPath语法 . 我在这里使用w3schools示例:

http://www.w3schools.com/xsl/tryit.asp?filename=try_xpath_select_pricenodes_high

..which基于以下XML:

<?xml version="1.0" encoding="UTF-8""?>

<bookstore>

<book category="COOKING">
  <title la="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>

该示例选择价格大于35的书籍 Headers . 我想使用此示例并选择类别名称而不是 Headers . 所以,我试过这个:

/bookstore/book[price>35]/@category

而且,正如您在该网站上自己测试一样,它不会产生任何输出 . 我错了什么?

谢谢你的时间 .

1 回答

  • 1

    查询没问题 . 但由于您处理属性节点而不是元素,因此必须调整打印结果的代码 . 改变线条

    document.write(nodes[i].childNodes[0].nodeValue);
    document.write(result.childNodes[0].nodeValue);
    

    document.write(nodes[i].nodeValue);
    document.write(result.nodeValue);
    

    你会得到预期的结果 .

相关问题