xml xpath 日期比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4347320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xpath dates comparison
提问by Razor
I'm trying to filter elements based on an attribute that is a date in the format yyyy-MM-dd.
我正在尝试根据格式为日期的属性过滤元素yyyy-MM-dd。
My XML looks like this:
我的 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>
My xpath attempt:
我的 xpath 尝试:
'//article[xs:date(./@pub-date) > xs:date("2010-11-15")]'
Using xpath 2.0 - would avoid adding a schema unless absolutely necessary.
使用 xpath 2.0 - 除非绝对必要,否则将避免添加架构。
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?
我相信那时我一定错过了一些东西。我是否可能需要为 xs:date 指定其他内容才能工作?也许是 xs: 命名空间定义?
回答by Dimitre Novatchev
In both XPath 1.0 and 2.0 you can use:
在 XPath 1.0 和 2.0 中,您都可以使用:
//article[number(translate(@pub-date,'-','')) > 20101115]
Your XPath 2.0 expression is correct, using Saxon 9.0.3 this transformation:
您的 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>
when applied on the provided XML document:
当应用于提供的 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>
produces the wanted, correct result:
产生想要的、正确的结果:
<article title="wired" pub-date="2010-11-22"/>
<article title="Plus 24" pub-date="2010-11-22"/>
回答by barry austra
<cfset startdatetime = Now() >
<cfset nNow = LSParseNumber(DateFormat(DateAdd('n', -15,startdatetime),'yyyyMMddHHmm')) >
number(substring(concat(translate(text(),'-: ',''),'0000000000000000'),1,12))<=#nNow#

