質問

I have a table with rows that contain dates of the form MM/DD/YYYY such as

<table>
  <tr><td>02/05/2013</td></tr>
  <tr><td>03/01/2013</td></tr>
  <tr><td>04/25/2013</td></tr>
  <tr><td>05/06/2013</td></tr>
  <tr><td>06/06/2013</td></tr>
</table>

I have two pieces of information to work with:

  • Month MM
  • Year YYYY

I want to match on the appropriate row with the given month and year values. It is guaranteed that there will be at most one entry per month, so it is not important to consider the day. If at some point the day is required, then the expression is trivial since I will have all three pieces of information.

I am using xpath 1.0 so it does not seem like I have access to regex in my expressions. What is a reliable way to get the appropriate row, given only the month and the year? For example, if I wanted the row that corresponds to June 2013, I don't want to accidentally match on 05/06/2013.

役に立ちましたか?

解決

I looked up the xpath functions and found that there was a starts-with function. So a solution, assuming I am looking for June 2013, would be

//tr[./td[starts-with(., '06') and contains(., '2013')]]

This would avoid issues with day/month ambiguity since the dates are always MM/DD/YYYY

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top