0

Below is my xml...

What I am trying to do is using XPATH and count expression.. I want a count of when a date in xml matches Todays date. So for example Todays Date is "2011-05-05".. Any messages that match this date have a count and return a integer. so answer would be NumberofTodaysMessages = 2.

<Response>
    <run_id>1</run_id>
    <message>
      <timestamp>2011-05-05T10:50:00.46875+00:00</timestamp>
      <event_type>Information</event_type>
      <operation>LoadProjects</operation>
      <error_code />
      <details>LoadProjects request detected</details>
    </message>
    <message>
      <timestamp>2011-05-05T10:50:02.296875+00:00</timestamp>
      <event_type>Error</event_type>
      <operation>Processor.InitaliseDCFiles</operation>
      <error_code />
      <details>some error details</details>
    </message>
    <message>
      <timestamp>2011-11-10T10:50:02.296875+00:00</timestamp>
      <event_type>Debug</event_type>
      <operation>Processor.InitaliseDCFiles</operation>
      <error_code />
      <details>some details</details>
    </message>
  <Response> 

How would I go about this in XPATH??

count(/Response/message/Timestamp[@DateTime.Now()])

user929153
  • 475
  • 2
  • 11
  • 25

2 Answers2

1

the xpath could be:

today = "2011-11-05" // replace by "today" in a string with your date library

xpath = "Response/message/timestamp[starts-with(text()='"+today+"')]"

AlbertFerras
  • 726
  • 5
  • 14
  • Could not do..... xpath = "Response/message/timestamp[starts-with(text()='"+DATETIME.NOW()+"')]" ?? Because I want to run this everyday.. – user929153 Nov 14 '11 at 16:52
  • build your xpath syntax using the string 'today' so you end with something like this: "Response/message/timestamp[starts-with(text()='2011-11-05')]" – AlbertFerras Nov 14 '11 at 16:53
  • so.... today = DATETIME.NOW().ToString() xpath = "Response/message/timestamp[starts-with(text()='"+today+"')]" – user929153 Nov 14 '11 at 16:55
0

C# implements XPath 2.0, right? Then you should have all these functions available.

forty-two
  • 12,204
  • 2
  • 26
  • 36
  • There is no XPath 2.0 implementation in .Net framework up to at least 4.0. Check out http://stackoverflow.com/questions/1525299/xpath-and-xslt-2-0-for-net if you need one. – Alexei Levenkov Nov 14 '11 at 18:11
  • OK, maybe I was misled by this statement: "The System.Xml.XPath namespace contains the classes that define a cursor model for navigating and editing XML information items as instances of the XQuery 1.0 and XPath 2.0 Data Model." of the MSDN library. – forty-two Nov 14 '11 at 21:09
  • Framework 4.5 supports XPath 2.0 - https://learn.microsoft.com/en-us/dotnet/api/system.xml.xpath?view=netframework-4.5 – Himanshu Patel Jan 26 '19 at 12:25