我试图根据一个属性筛选元素,该属性是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 to的其他内容
工作?可能是xs:namespace
定义?
最佳答案
在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"/>
关于xml - xpath日期比较,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4347320/