2012-08-28 83 views
1

我想使用XPath檢索位於另一個DIV中格節點的節點:檢索XPath來選擇它的父節點包含Java腳本

<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[@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包含腳本。任何人都可以幫助我弄清楚這個問題。

回答

2

像這樣簡單

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

這將選擇任何div,字符串值,其class屬性"linkTrail",這是一個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表達式求值和評價的結果(所選擇的節點(S))被複制到輸出

<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> 
0

在您的XPath中,您指定@class爲'工具欄',但它是具有該值的@id。如果我調整表達式,我會得到所需的輸出結果以及「清除」div。

問題可能是div被包含在與條件匹配的另一個div中。