19

我正在尝试根据格式为日期的属性过滤元素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: 命名空间定义?

4

2 回答 2

30

在 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 文档时

<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"/>
于 2010-12-03T18:22:54.470 回答
2
<cfset startdatetime = Now() >
<cfset nNow = LSParseNumber(DateFormat(DateAdd('n', -15,startdatetime),'yyyyMMddHHmm')) >

number(substring(concat(translate(text(),'-: ',''),'0000000000000000'),1,12))<=#nNow#
于 2011-10-06T14:20:20.697 回答