xpath 日期比较 [英] xpath dates comparison
本文介绍了xpath 日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据 yyyy-MM-dd
格式的日期属性过滤元素.
I'm trying to filter elements based on an attribute that is a date in the format yyyy-MM-dd
.
我的 XML 如下所示:
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>
我的 xpath 尝试:
My xpath attempt:
'//article[xs:date(./@pub-date) > xs:date("2010-11-15")]'
使用 xpath 2.0 - 除非绝对必要,否则将避免添加架构.
Using xpath 2.0 - would avoid adding a schema unless absolutely necessary.
来自评论:
我相信我一定遗漏了什么然后.有没有可能我需要为 xs:date 指定其他内容工作?也许是 xs: 命名空间定义?
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?
推荐答案
在 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"/>
这篇关于xpath 日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文