如何在html头中选择xpath脚本?

时间:2011-01-23 14:09:07

标签: xpath

如何在html head中选择xpath脚本?

示例(如何仅选择ThisFile1.js?):

<html>
<head>
<script type="text/javascript" src="ThisFile1.js"></script>
</head>
<body>
<script type="text/javascript" src="NotThisFile1.js"></script>
<script type="text/javascript" src="NotThisFile2.js"></script>
<script type="text/javascript" src="NotThisFile3.js"></script>
<script type="text/javascript" src="NotThisFile4.js"></script>
</body>
</html>

谢谢, 约瑟夫

1 个答案:

答案 0 :(得分:5)

使用

/*/head/script/@src

string(/*/head/script/@src)

第一个XPath表达式选择所有src元素的所有script属性,这些元素是所有head元素的子元素,这些元素是顶级元素的子元素XML文档。

第二个XPath表达式计算为第一个XPath表达式选择的第一个属性的字符串值。如果您的XPath引擎的API支持评估不选择节点的XPath表达式,那么这将是最方便的。如果不是这种情况,则必须使用第一个XPath表达式,然后使用API​​的相应方法/属性返回所选属性节点的字符串值。