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>

linktrail divを取得するために、xpathに続いてxpathを試しました:

//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']

これは、属性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 式が評価され、評価の結果 (選択されたノード) が出力にコピーされます

<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 'toolbar' と指定します@idが、値を持つのは です。式を調整すると、目的の出力に加えて「クリア」な div が得られます。

問題は、条件に一致するdiv別のものに囲まれている可能性があります。div

于 2012-08-28T09:54:13.633 に答える