我想使用xpath检索位于另一个div内的div节点:
我的意见是:
<div id="toolbar">
<div class="linkTrail">
<span class="trailLabel">You are here: </span>
<a href="http://www.euromonitor.com/home">Home</a>
<a href="http://www.euromonitor.com/solutions">Solutions</a>
<a href="http://www.euromonitor.com/industries">Industries</a>
<a href="http://www.euromonitor.com/home-care" class="last">Home Care</a>
</div>
<script type="text/javascript">var switchTo5x = false;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript"> stLight.options({ publisher: 'c4fa7e97-6938-4efa-b3d0-b81551cc9ee3', tracking: 'google' });</script>
<div class="clear">
</div>
我希望输出为:
<div class="linkTrail">
<span class="trailLabel">You are here: </span>
<a href="http://www.euromonitor.com/home">Home</a>
<a href="http://www.euromonitor.com/solutions">Solutions</a>
<a href="http://www.euromonitor.com/industries">Industries</a>
<a href="http://www.euromonitor.com/home-care" class="last">Home Care</a>
</div>
我在xpath之后尝试了xpath来检索linktrail div:
//div[@class='toolbar']/div
但是它给了我错误的输出:
<div class="linkTrail">
<span class="trailLabel">You are here: </span>
<a href="http://www.euromonitor.com/home">Home</a>
<a href="http://www.euromonitor.com/solutions">Solutions</a>
<a href="http://www.euromonitor.com/industries">Industries</a>
<a href="http://www.euromonitor.com/home-care" class="last">Home Care</a>
</div>
<script type="text/javascript">var switchTo5x = false;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript"> stLight.options({ publisher: 'c4fa7e97-6938-4efa-b3d0-b81551cc9ee3', tracking: 'google' });</script>
可能是什么问题,是由于父div包含脚本。任何人都可以帮我解决这个问题。
答案 0 :(得分:2)
就这么简单:
//div[@id='toolbar']/div[@class='linkTrail']
这会选择任何div
,其class
属性为"linkTrail"
的字符串值,且div
属性为字符串id
的子项值"toolbar"
基于XSLT的验证:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="//div[@id='toolbar']/div[@class='linkTrail']"/>
</xsl:template>
</xsl:stylesheet>
将此转换应用于提供的XML文档时:
<div id="toolbar">
<div class="linkTrail">
<span class="trailLabel">You are here: </span>
<a href="http://www.euromonitor.com/home">Home</a>
<a href="http://www.euromonitor.com/solutions">Solutions</a>
<a href="http://www.euromonitor.com/industries">Industries</a>
<a href="http://www.euromonitor.com/home-care" class="last">Home Care</a>
</div>
<script type="text/javascript">var switchTo5x = false;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript"> stLight.options({ publisher: 'c4fa7e97-6938-4efa-b3d0-b81551cc9ee3', tracking: 'google' });</script>
<div class="clear"></div></div>
评估Xpath表达式并将评估结果(所选节点)复制到输出:
<div class="linkTrail">
<span class="trailLabel">You are here: </span>
<a href="http://www.euromonitor.com/home">Home</a>
<a href="http://www.euromonitor.com/solutions">Solutions</a>
<a href="http://www.euromonitor.com/industries">Industries</a>
<a href="http://www.euromonitor.com/home-care" class="last">Home Care</a>
</div>
答案 1 :(得分:0)
在XPath中,您将@class
指定为“工具栏”,但@id
具有该值。如果我调整表达式,我会得到你想要的输出加上“clear”div。
问题可能是div
包含在与条件匹配的另一个div
中。