这里有两个解决方案:
一、XPath 1.0
这是一对XPath 1.0 http://www.w3.org/TR/xpath表达式选择所需的节点:
/*/*
[translate(@time, ':','')
>
translate('15:59',':','')
][1]
选择第一个sub
时间晚于的节点15:59
.
/*/*
[translate(@time, ':','')
<
translate('15:59',':','')
][last()]
选择 选择第一个sub
与前一个节点相比15:59
sub
time.
我们可以将这些包含在 XSLT 转换中并检查是否产生了真正想要的结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
First time after 15:59:
<xsl:copy-of select=
"/*/*
[translate(@time, ':','')
>
translate('15:59',':','')
][1]
"/>
First time before 15:59:
<xsl:copy-of select=
"/*/*
[translate(@time, ':','')
<
translate('15:59',':','')
][last()]
"/>
</xsl:template>
</xsl:stylesheet>
当应用上述变换时在最初提供的 XML 文档上:
<mainNode>
<sub time="08:00">
<status id="2">On</status>
<status id="3">Off</status>
</sub>
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
<sub time="20:00">
<status id="4">Off</status>
<status id="7">On</status>
</sub>
<sub time="23:59">
<status id="4">On</status>
<status id="7">On</status>
</sub>
</mainNode>
产生了想要的结果:
First time after 15:59:
<sub time="16:00">
<status id="5">On</status>
<status id="6">On</status>
<status id="7">Off</status>
<status id="8">On</status>
</sub>
First time before 15:59:
<sub time="13:00">
<status id="4">On</status>
<status id="7">On</status>
</sub>
Do note下列:
XPath 的使用translate() http://www.w3.org/TR/xpath#function-translate功能去掉冒号
使用last() http://www.w3.org/TR/xpath#function-last功能在第二个表达式中
无需将时间转换为秒比较前
当用作 XML 文档(例如 XSLT 样式表)的一部分时,the <
运算符必须转义.
二. XPath 2.0
In XPath 2.0 http://www.w3.org/TR/xpath20/我们可以使用以下两个表达式生成选择所需的节点:
/*/*[xs:time(concat(@time,':00'))
gt
xs:time('15:59:00')
][1]
选择第一个sub
时间晚于的节点15:59
.
/*/*[xs:time(concat(@time,':00'))
lt
xs:time('15:59:00')
][last()]
选择 选择第一个sub
与前一个节点相比15:59
sub
time.
我们可以将这些包含在 XSLT 2.0 转换中并检查是否产生了真正想要的结果:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
First time after 15:59:
<xsl:copy-of select=
"/*/*[xs:time(concat(@time,':00'))
gt
xs:time('15:59:00')
][1]
"/>
First time before 15:59:
<xsl:copy-of select=
"/*/*[xs:time(concat(@time,':00'))
lt
xs:time('15:59:00')
][last()]
"/>
</xsl:template>
</xsl:stylesheet>
当应用上述变换时在最初提供的 XML 文档上(与第一个解决方案相同),会产生相同的想要的结果。
Do note下列:
-
在 XPath 2.0 中xs:time http://www.w3.org/TR/xmlschema-2/#time是本机数据类型。然而,为了构建一个
xs:time()
根据 xml 文档中的值,我们必须将缺少的秒部分连接到它们。
-
在 XPath 2.0 中
xs:time
值可以与“原子值比较运算符 http://www.w3.org/TR/xpath-functions/#comp.duration.datetime"例如lt
or gt
.