XPath根据标签选择表格单元格?

时间:2018-02-19 23:02:53

标签: html xml xpath

我有以下HTML,因此我需要获得Available QTY值10.000。需要在c#中使用XPath。

我已经尝试了下面的XPath,但它没有获取结果。任何帮助表示赞赏。

HtmlAgilityPack.HtmlNode node = 
  doc.DocumentNode.SelectSingleNode("//td[b[. = 'Available Qty          :']]/following-sibling::td");

HTML

<html>
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<link href="/public/vista/york.css" type="text/css" rel="stylesheet">
</head>
<form name="myform" method="post">
<input type="HIDDEN" name="PLANT" value="">
<input type="HIDDEN" name="session_id" value="ZiAlfPry4VkUj2*wTbMql***VISTA312111$-21~-19868108">
<input type="HIDDEN" name="user_id" value="vijay">
<table border="0" width="620">
    <tr>
        <td class="title" colspan="4">
            Product Availability     
        </td>
    </tr>

    <tr>
        <td class="text" colspan="4">
            <img border="0" src="/public/bc/icons/s_b_info.gif">
            &nbsp;Quantities reflect forecasted production within time periods displayed       <br>
            <img border="0" src="/public/bc/icons/s_b_info.gif">
            &nbsp;For any questions or concerns regarding your product availability please contact your Customer Service Representative at 800-536-6112             
        </td>
    </tr>
    <tr class="text">
        <td colspan="4">
            &nbsp;
        </td>
    </tr>

    <tr class="text">
        <td width="20%">
            <b>Customer ID     :</b>
        </td>
   <td width="30%">
       0010015272
   </td>
   <td width="20%">
       <b>Customer Name     :</b>
   </td>
   <td width="30%">
       AIR COND. UTILITIES.
   </td>
    </tr>
    <tr class="text">
        <td>
       <b>Item Number     :</b>
   </td>
   <td>
       ND120C00C6AAA3
   </td>
   <td>
       <b>Item Description     :</b>
   </td>
   <td>
       10T_EVAP_230/460-3-60_R410A_
   </td>
    </tr>
    <tr class="text">
        <td>
            <b>Dimensions     :</b>
        </td>
        <td>
            L030:W056:H065 - IN
        </td>
        <td>
            <b>Price Family     :</b>
        </td>
        <td>
            2720
        </td>
    </tr>
    <tr class="text">
        <td valign="top">
            <b>Weight     :</b>
        </td>
        <td valign="top">
            562.000 &nbsp;(LB)
        </td>
        <td>
            <b>UPC Code     :</b>
        </td>
        <td>
            883982733421
        </td>
    </tr>
    <tr class="text">
        <td valign="top">
            <b>Ship From Plant     :</b>
        </td>
        <td valign="top">
            US UPG Norman
        </td>
        <td colspan="2" valign="top">
            <table border="0">

                            <tr class="text">
                                <td class="tableCommon">
                                    <b>Brand     </b>
                                </td>

                                    <td class="tableCommon">
                                        <b>List Price     </b>
                                    </td>


                                    <td class="tableCommon">
                                        <b>Net Price     </b>
                                    </td>


                                    <td class="tableCommon">
                                        <b>Discount       </b>
                                    </td>

                            </tr>

                        <tr class="text">
                            <td>
                                YORK
                            </td>

                            <td align="right">
                                5330.00 
                            </td>


                            <td align="right">
                                2132.00 
                            </td>


                            <td align="right">
                                60.00-
                            </td>

                        </tr>

            </table>
        </td>
    </tr>
    <!--
    <tr class="text">
        <td colspan="4">
            <b>Replenishment Lead Time     :</b>&nbsp;
            16 &nbsp;
            <b>Factory Working Days     </b>
        </td>
    </tr>
    -->
<!-- begin of changes for task 3536 akumars11 16oct2007 -->
<!--  <td colspan="2"></td> -->
    <tr class="text">
        <td>
            <b>Min. Order Quantity :</b>
        </td>
        <td>
            0.000 
        </td>
 <!-- begin of changes for task 3536 akumars11 16oct2007 -->
    <tr class="text">
        <td>
    <!-- Begin of changes for SM 4000008069 (Change the name balance on hand to available qty) -->

            <font size='3'> <b>Available Qty          :</b> </font>
              <!-- Insertion for SM 4000008069-->
            <!-- End of insertion for SM 4000008069 -->
        </td>
        <td>

            <font size='3'> 10.000  </font>
               <!-- End of insertion for SM 4000007790 -->
        </td>
     </tr>
        <!-- (Add current lead time and delivery date) -->
    <!--insertion for SM 4000007790 -->
    <tr class="text">
        <td>
            <font size='3'> <b>Current Lead Time          :</b> </font>
        </td>
        <td>
            <font size='3'> 26 Days </font>
        </td>
        <!-- Display the traffic light based on standard time and current lead time difference)  -->
        <td>


        <img src="flash_red.gif" border="0" width="70" height="80">


        </td>
    </tr>
    <tr class="text">
        <td>
            <font size='3'> <b>Est. Delivery Date          :</b> </font>
        </td>
        <td>
            <font size='3'> 2018/03/17 </font>
        </td>
    </tr>
     <!-- Insertion for SM 4000007790-->
   <!-- End of insertion for SM 4000008069 -->

    <tr class="text">
        <td>

        <font size='3'><b>ABC indicator:</b></font>
          <!-- End of Insertion for SM 4000007790-->
        </td>
        <td>

        <font size='3'>A</font> <!-- Font added for SM 4000007790 -->
          <!-- End of Insertion for SM 4000007790-->
        </td>
  <!-- Begin of changes for SM 4000007790 -->
    </tr>

     <tr>
        <td class="text" colspan="4">
            <font color='red'>*If Requested Delivery Date is earlier than Est. Delivery Date contact your Order Services Account Manager for confirmation.                                                              </font>
        </td>
     </tr>
     <tr>
        <td class="text" colspan="4">
             <font color='red'>*Deliveries requested less than 15 days are subject to standard freight policies and Lead Time surcharges.</font>
        </td>
     </tr>

  <!-- End of changes for SM 4000007790-->
  <!-- begin of changes for SM 4000004032 -->
 <!--Begin of insertion for SM 4000007790-->
 <tr>

    <td>
    <table border="2">
       <!--Insertion for SM 4000007790-->
               <tr class="text">
                     <td class="tableCommon">
                        <b>ABC indicator</b>
                      </td>
                      <td class="tableCommon">
                        <b>Description          </b>
                       </td>
                       <td class="tableCommon">


                               <b>Standard Lead Time  </b>

                           <!-- End of changes for SM 4000007790-->

                        </td>
               </tr>


                        <tr class="text">
                            <td>
                                A
                            </td>
                            <td align="left">
                                Make to Stock
                            </td>
                            <td align="left">
                                15
                            </td>
                        </tr>


                        <tr class="text">
                            <td>
                                B
                            </td>
                            <td align="left">
                                Assemble to Order
                            </td>
                            <td align="left">
                                30
                            </td>
                        </tr>


                        <tr class="text">
                            <td>
                                C
                            </td>
                            <td align="left">
                                Make to Order
                            </td>
                            <td align="left">
                                45
                            </td>
                        </tr>


                        <tr class="text">
                            <td>
                                D
                            </td>
                            <td align="left">
                                Obsolete, Discontinued
                            </td>
                            <td align="left">
                                15
                            </td>
                        </tr>


                        <tr class="text">
                            <td>
                                P
                            </td>
                            <td align="left">
                                Phased Out, Availability limited to current stock
                            </td>
                            <td align="left">
                                15
                            </td>
                        </tr>


                        <tr class="text">
                            <td>
                                T
                            </td>
                            <td align="left">
                                Coated Coil
                            </td>
                            <td align="left">
                                45
                            </td>
                        </tr>


                        <tr class="text">
                            <td>
                                V
                            </td>
                            <td align="left">
                                Vendor Direct ship
                            </td>
                            <td align="left">
                                45
                            </td>
                        </tr>

            </table>
        </td>
  <!-- end of changes for SM 4000004032 -->
   </tr>







    <!--
    <tr class="text">
        <td colspan="4">
            &nbsp;
        </td>
    </tr>
    <tr class="text">
        <td colspan="4">

            &nbsp;&nbsp;<B>Summary     </B>&nbsp;&nbsp;


                &nbsp;&nbsp;
                <INPUT id=button2 type=submit value="US UPG Norman" name="onInputProcessing(1001)">
                &nbsp;&nbsp;

                &nbsp;&nbsp;
                <INPUT id=button2 type=submit value="US UPG Wichita" name="onInputProcessing(1002)">
                &nbsp;&nbsp;

                &nbsp;&nbsp;
                <INPUT id=button2 type=submit value="US UPG Source 1" name="onInputProcessing(1003)">
                &nbsp;&nbsp;

                &nbsp;&nbsp;
                <INPUT id=button2 type=submit value="US VRF Line of Business" name="onInputProcessing(1011)">
                &nbsp;&nbsp;

                &nbsp;&nbsp;
                <INPUT id=button2 type=submit value="Small Tonnage Chillers" name="onInputProcessing(1012)">
                &nbsp;&nbsp;

                &nbsp;&nbsp;
                <INPUT id=button2 type=submit value="" name="onInputProcessing(0165)">
                &nbsp;&nbsp;

        </td>
    </tr>
    -->

<!-- Begin of comment for SM 4000008069 -->
  <!-- Insertion for SM 4000007790 -->


<!-- End of comment for SM 4000008069 -->
    <tr class="text">
        <td colspan="4">
            <hr>
        </td>
    </tr>
    <tr>
        <td class="text">
            <b>Item Number     </b>
        </td>
        <td class="text">
            <input maxlength="18" size="18" name="txtitemnumber" value="ND120C00C6AAA3">
        </td>
        <td colspan="2" class="text">
            <input id="button4" type="submit" value="    Download to Excel           " name="onInputProcessing(submit)">
            &nbsp;
            <input id="button4" type="submit" value="Search " name="onInputProcessing(search)">
        </td>
    </tr>

    <tr>
        <td colspan="3" class="text" align="center">

                <input id="button1" type="submit" value="Previous - ND120C00C5AZZ3" name="onInputProcessing(prev)">

            &nbsp;

                <input id="button4" type="submit" value="Next - ND120C00C6AZZ3" name="onInputProcessing(next)">

        </td>
        <td></td>
    </tr>



    <tr class="TEXT">
        <td class="text" colspan="4">
            <br>
            <input id="button3" type="submit" value="Back " name="onInputProcessing(back)">
        </td>
    </tr>
    <tr class="text">
        <td class="smallstatementtext" colspan="4" align="right">
            100
        </td>
    </tr>
</tr></table>
<input type="hidden" name="pl" value="1001;1002;1003;1011;1012;0165">
<input type="hidden" name="slsorg" value="US15">
<input type="hidden" name="txtqtyreq" value="999999">
<input type="hidden" name="nxtMdl" value="ND120C00C6AZZ3">
<input type="hidden" name="prvMdl" value="ND120C00C5AZZ3">
</form>

</html>

2 个答案:

答案 0 :(得分:1)

如果您提供表名等Example:

之类的完整路径,也许会有所帮助
<table border="0" cellpadding="0" cellspacing="0" id="table2">
    <tr>
        <th>Name
        </th>
        <th>Age
        </th>
    </tr>
        <tr>
        <td>Mario
        </td>
        <th>Age: 78
        </td>
    </tr>
            <tr>
        <td>Jane
        </td>
        <td>Age: 67
        </td>
    </tr>
            <tr>
        <td>James
        </td>
        <th>Age: 92
        </td>
    </tr>
</table>

foreach (var cell in doc.DocumentNode.SelectNodes("//table[@id='table2']/tr/td"))
{
    Response.Write(cell.InnerText);
}

答案 1 :(得分:1)

这个XPath,

normalize-space(//td[normalize-space()='Available Qty :']/following-sibling::td[1])

将返回td的空格规范化字符串值,该值紧跟在td之后,其空格规范化字符串值是目标标​​签,

10.000

按要求。