how to write this XPATH for search?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flyingbear
    New Member
    • Sep 2007
    • 7

    how to write this XPATH for search?

    I have a xml file:
    <AAA id="1111">
    <DateTime>200 7-01-01 12:01:03</DateTime>
    ....
    </AAA>

    How can I use XPath to select all <AAA> nodes whose DateTime value is 30 days older than the current DateTime?

    Or, I have to use a loop?


    thanks

    bear
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    Hi,

    While I am aware of a date object in EXSLT (an extension to XSLT) I don't think that there is any date handling functionality included in XPATH.

    The only way that comes to mind immediately is if you had a method of numbering the days from a specific time. Each date could then have an equivalent day number which can be added to the datetime node as an attribute, you could then use a predicate on this attribute to select the relevant nodes. To implement this you would need to be able to manipulate the XML creation.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Does it HAVE to be exactly 30 days? Or is a month good enough?

      1. You'd need to pass the current date as a parameter into the xslt.
      2. Separate the current date into $cyear, $cmonth, $cday
      3. You can seperate AAA Datetime into year, month, day with xpaths:
      year: "substring(Date Time,1,4)"
      month: "substring(Date Time,6,2)"
      day: "substring(Date Time,9,2)"

      4. Use the following logic:
      [code=txt]
      ($cyear < year - 1) or
      ($cyear = year - 1 and (
      ($cmonth > 1 or month < 12) or
      ($cday < day)
      )) or
      ($cyear = year and (
      ($cmonth < month - 1) or
      ($cmonth = month - 1 and $cday < day)
      ))
      [/code]

      If you are selecting all of these nodes for the sake of processing, it would probably be easier to apply this filter inside of the template, or for-each statement, since you could create temporary variables for the year, month and day.

      Comment

      Working...