1
votes

I want to retrieve div node located inside another div using xpath :

My input is:

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

I want output as:

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

I tried xpath following xpath to retrive linktrail div:

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

But its giving me wrong output:

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

what can be the issue, is it due to parent div contains script. Can anyone help me to figure out this problem.

2

2 Answers

2
votes

As simple as this:

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

This selects any div, the string value of whose class attribute is "linkTrail" and that is a child of a div whose id attribute has string value "toolbar"

XSLT-based verification:

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

When this transformation is applied to the provided XML document:

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

the Xpath expression is evaluated and the result of the evaluation (the selected node(s)) is copied to the output:

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

In your XPath, you specify @class to be 'toolbar', but it is the @id that has the value. I am getting your desired output plus the "clear" div if I adjust the expression.

The problem might be the div is enclosed in another div that matches the condition.