xpath 日期比较

发布于 2024-10-06 01:44:52 字数 633 浏览 0 评论 0原文

我正在尝试根据格式为 yyyy-MM-dd 的日期属性来过滤元素。

我的 XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <article title="wired" pub-date="2010-11-22" />
  <article title="Plus 24" pub-date="2010-11-22" />
  <article title="Finance" pub-date="2010-10-25" />
</root>

我的 xpath 尝试:

'//article[xs:date(./@pub-date) > xs:date("2010-11-15")]'

使用 xpath 2.0 - 除非绝对必要,否则将避免添加架构。

来自评论

我相信我一定错过了一些东西 然后。我是否有可能需要 为 xs:date 指定其他内容 工作?也许是 xs: 命名空间 定义?

I'm trying to filter elements based on an attribute that is a date in the format yyyy-MM-dd.

My XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <article title="wired" pub-date="2010-11-22" />
  <article title="Plus 24" pub-date="2010-11-22" />
  <article title="Finance" pub-date="2010-10-25" />
</root>

My xpath attempt:

'//article[xs:date(./@pub-date) > xs:date("2010-11-15")]'

Using xpath 2.0 - would avoid adding a schema unless absolutely necessary.

From comments:

I believe I must be missing something
then. Is it possible that I need to
specify something else for xs:date to
work? Maybe a xs: namespace
definition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毅然前行 2024-10-13 01:44:52

在 XPath 1.0 和 2.0 中您都可以使用

//article[number(translate(@pub-date,'-','')) > 20101115]

您的 XPath 2.0 表达式是正确的,使用 Saxon 9.0.3 进行此转换

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:template match="/">
      <xsl:sequence select="//article[xs:date(./@pub-date) > xs:date('2010-11-15')]"/>
    </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时< /strong>:

<root>
  <article title="wired" pub-date="2010-11-22" />
  <article title="Plus 24" pub-date="2010-11-22" />
  <article title="Finance" pub-date="2010-10-25" />
</root>

产生想要的正确结果

<article title="wired" pub-date="2010-11-22"/>
<article title="Plus 24" pub-date="2010-11-22"/>

In both XPath 1.0 and 2.0 you can use:

//article[number(translate(@pub-date,'-','')) > 20101115]

Your XPath 2.0 expression is correct, using Saxon 9.0.3 this transformation:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:template match="/">
      <xsl:sequence select="//article[xs:date(./@pub-date) > xs:date('2010-11-15')]"/>
    </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<root>
  <article title="wired" pub-date="2010-11-22" />
  <article title="Plus 24" pub-date="2010-11-22" />
  <article title="Finance" pub-date="2010-10-25" />
</root>

produces the wanted, correct result:

<article title="wired" pub-date="2010-11-22"/>
<article title="Plus 24" pub-date="2010-11-22"/>
柏拉图鍀咏恒 2024-10-13 01:44:52
<cfset startdatetime = Now() >
<cfset nNow = LSParseNumber(DateFormat(DateAdd('n', -15,startdatetime),'yyyyMMddHHmm')) >

number(substring(concat(translate(text(),'-: ',''),'0000000000000000'),1,12))<=#nNow#
<cfset startdatetime = Now() >
<cfset nNow = LSParseNumber(DateFormat(DateAdd('n', -15,startdatetime),'yyyyMMddHHmm')) >

number(substring(concat(translate(text(),'-: ',''),'0000000000000000'),1,12))<=#nNow#
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文