【问题标题】:Xpath count and compare datetimeXpath 计数和比较日期时间
【发布时间】:2011-12-28 19:45:10
【问题描述】:

下面是我的xml...

我想要做的是使用 XPATH 和计数表达式。我想要计算 xml 中的日期何时与今天的日期匹配。因此,例如今天的日期是“2011-05-05”.. 任何与此日期匹配的消息都有一个计数并返回一个整数。所以答案是 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> 

我将如何在 XPATH 中解决这个问题??

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

【问题讨论】:

    标签: c# xml datetime xpath count


    【解决方案1】:

    xpath 可以是:

    tod​​ay = "2011-11-05" // 用您的日期库替换字符串中的“today”

    xpath = "响应/消息/时间戳[starts-with(text()='"+today+"')]"

    【讨论】:

    • 不能做..... xpath = "Response/message/timestamp[starts-with(text()='"+DATETIME.NOW()+"')]" ??因为我想每天都运行这个..
    • 使用字符串 'today' 构建你的 xpath 语法,所以你会以这样的方式结束:“Response/message/timestamp[starts-with(text()='2011-11-05')] "
    • so.... 今天 = DATETIME.NOW().ToString() xpath = "Response/message/timestamp[starts-with(text()='"+today+"')]"
    【解决方案2】:

    C# 实现了 XPath 2.0,对吗?那么你应该拥有所有这些functions 可用。

    【讨论】:

    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多