1

我想使用 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 包含脚本。谁能帮我解决这个问题。

4

2 回答 2

2

就这么简单

//div[@id='toolbar']/div[@class='linkTrail']

这将选择 any div,其class属性的字符串值是"linkTrail"并且是 a 的子属性,divid属性具有字符串值"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>
于 2012-08-28T12:51:21.097 回答
0

在您的 XPath 中,您指定@class 为“工具栏”,但它是@id具有值的。如果我调整表达式,我会得到你想要的输出加上“清晰”的 div。

问题可能是包含在与条件匹配的div另一个中。div

于 2012-08-28T09:54:13.633 回答