在Categories后面用XPath解析HTML

本文关键字:XPath 解析 HTML Categories | 更新日期: 2023-09-27 17:58:33

我有下面的HTML结构,每个tr标记都是相互分隔的,所以当我尝试用XPATH进行解析时,它应该只为一个类别有两个子项,但在下面的代码中,它将所有4个子项选入一个类别,所以每个类别都有4个子项,而不是只有2个子项。

<table class="available">
   <tbody>
      <tr>
         <td class="catname" colspan="2">
            <span>Category 1</span>
         </td>
      </tr>
      <tr>
         <td rowspan="2" colspan="1" class="itemdetail">
            <div class="subname">
               SubItem1-1
            </div>
         </td>
         <td class="precioseleccion desgloseth">
            <div class="preprice">
               <strong class="price">39.99 €</strong>
            </div>
         </td>
      </tr>
      <tr>
         <td rowspan="2" colspan="1" class="itemdetail">
            <div class="subname">
               SubItem1-2
            </div>
         </td>
         <td class="precioseleccion desgloseth">
            <div class="preprice">
               <strong class="price">49.99 €</strong>
            </div>
         </td>
      </tr>
      <tr>
         <td class="catname" colspan="2">
            <span>Category 2</span>
         </td>
      </tr>
      <tr>
         <td rowspan="2" colspan="1" class="itemdetail">
            <div class="subname">
               SubItem2-1
            </div>
         </td>
         <td class="precioseleccion desgloseth">
            <div class="preprice">
               <strong class="price">59.99 €</strong>
            </div>
         </td>
      </tr>
      <tr>
         <td rowspan="2" colspan="1" class="itemdetail">
            <div class="subname">
               SubItem2-2
            </div>
         </td>
         <td class="precioseleccion desgloseth">
            <div class="tooltip3">
               <strong class="price">69.99 €</strong>
            </div>
         </td>
      </tr>
    </tbody>    
</table>
var doc = new HtmlDocument(); // with HTML Agility pack
            doc.LoadHtml(uricontent);
            var rooms = doc.DocumentNode
           .SelectNodes("//table[@class='available']//td[@class='catname']")
           .Select(r => new
           {
               Type= r.InnerText.CleanInnerText(),
               SubTypes= r.SelectNodes("../..//tr//td[@class='itemdetail']//div[@class='subname']")
                            .Select(s => new
                            {
                                SubType= s.InnerText.CleanInnerText(),
                                Price =  
                                    s.SelectSingleNode(".//parent::td/following-sibling::td[@class='allprice']//div[@class='preprice']//strong[@class='price']")
                                        .InnerText.CleanInnerText()
                            }).ToArray()
           }).ToArray();

在Categories后面用XPath解析HTML

如果我正确理解您的问题,请选择您想要的//tr[td[@class='catname']]的所有类别,并选择您想要following-sibling::tr/td[div[@class='subname']]的子项目。