简体   繁体   中英

Ruby Nokogiri - XPATH using URL

I have this table:

 <tr>
      <td><b>Amount</b></td>
      <td><b>Due Date</b></td>
      <td"><b>Link</b></td>
  </tr>

<tr>
  <td>02/13/2012</td>
  <td>$81.66</td>
  <td><a onclick="javascript:window.open('/cso/displaypdfbill?selectedBillkey=449409587','_blank');" href="javascript: void(0);">View Bill</a></td>

</tr>
<tr>
  <td>01/13/2012</td>
  <td>$181.66</td>
  <td><a onclick="javascript:window.open('/cso/displaypdfbill?selectedBillkey=543409587','_blank');" href="javascript: void(0);">View Bill</a></td>

</tr>

I am looping through the table and extracting the Bill key in each row. I removed the Billkey and stored it into a variable.

BillKey = 449409587

What I want is to get the <tr> where that BillKey is located:

So I should have:

2/13/2012      81.86     View Bill

I am having trouble writing the XPATH to get the <tr> .

Use :

   string(table/tr
            [td/a/@onclick
                  [substring
                    (.,
                     string-length()
                   - 21
                    )
                  =
                   $vEnding
                  ]
            ]
         )

where $vEnding must be substituted by the string : "=449409587','_blank');"

So, the complete XPath expression after this substitution is:

   string(table/tr
            [td/a/@onclick
                  [substring
                    (.,
                     string-length()
                   - 21
                    )
                  =
                   "=449409587','_blank');"
                  ]
            ]
         )

XSLT - based verification :

This XSLT transformation :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vEnding">=449409587','_blank');</xsl:variable>

 <xsl:template match="/">
  <xsl:copy-of select=
  "string(table/tr
            [td/a/@onclick
                  [substring
                    (.,
                     string-length()
                   - 21
                    )
                  =
                   $vEnding
                  ]
            ]
         )
  "/>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document (the provided one wrapped in a single top element table ):

<table>
    <tr>
        <td>
            <b>Amount</b>
        </td>
        <td>
            <b>Due Date</b>
        </td>
        <td>
            <b>Link</b>
        </td>
    </tr>
    <tr>
        <td>02/13/2012</td>
        <td>$81.66</td>
        <td>
            <a onclick=
            "javascript:window.open('/cso/displaypdfbill?selectedBillkey=449409587','_blank');" href="javascript: void(0);">View Bill</a>
        </td>
    </tr>
    <tr>
        <td>01/13/2012</td>
        <td>$181.66</td>
        <td>
            <a onclick=
            "javascript:window.open('/cso/displaypdfbill?selectedBillkey=543409587','_blank');" href="javascript: void(0);">View Bill</a>
        </td>
    </tr>
</table>

evaluates the XPath expression and copies to the output the result of the evaluation :

    02/13/2012
    $81.66

        View Bill

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM