如何在XPath中排除特定的文本值?

时间:2015-09-01 16:36:14

标签: html css ruby xpath nokogiri

我想展示DEF,不包括ABC:

<ul class="view5">
    <li>
        <a href="/prd/detail_cate.jsp?item_cd=35386060" class="prdLink">
                <span class="prd_wrap">
                <img src="//itemimage.cjmall.com/goods_images/35/060/35386060J.jpg" alt="" onerror='this.src="http://itemimage.cjmall.com/goods_images/imageJ.jpg"'/>
                &nbsp;</span>
            <span class="prdTit">
                <B><FONT COLOR=#FF0000>ABC</FONT></B>DEF
            </span>
        </a>
    </li>
</ul>

我的代码是:

@html.xpath('//ul[contains(@class,"view5")]//li//a[contains(@class,"prdLink")]//span[contains(@class,"prdTit")]').text

如何修改我的代码以显示'DEF',不包括'ABC'?

1 个答案:

答案 0 :(得分:0)

你使它变得比它更难。如果要显示“DEF”,则删除“ABC”。我会这样做:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<ul class="view5">
    <li>
        <a href="/prd/detail_cate.jsp?item_cd=35386060" class="prdLink">
            <span class="prdTit">
                <B><FONT COLOR=#FF0000>ABC</FONT></B>DEF
            </span>
        </a>
    </li>
</ul>
EOT

doc.at('.prdTit b').remove
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> <ul class="view5">
# >>     <li>
# >>         <a href="/prd/detail_cate.jsp?item_cd=35386060" class="prdLink">
# >>             <span class="prdTit">
# >>                 DEF
# >>             </span>
# >>         </a>
# >>     </li>
# >> </ul>
# >> </body></html>