加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – xpath日期比较

发布时间:2020-12-16 07:55:54 所属栏目:百科 来源:网络整理
导读:我试图根据一个日期格式为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
我试图根据一个日期格式为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 – 除非绝对必要,否则将避免添加模式.

从评论:

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"/>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读