Xpath查询和时间

2023-12-24

您好,这是我的 XML 文件的内容:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>

我的程序获取当前时间: 如果我得到 15.59,我必须检索下一个子时间(16.00)的所有状态 ID:

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

通过第二个 XPath 查询,我必须获取前一个子时间 (13.00) 的所有状态 ID。 怎么做?我了解 SQL,但对 XPath 还很陌生。我也接受重要的 XPath 资源的 URL(如果有的话)。谢谢!


这里有两个解决方案:

一、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, ':','') 
         &lt; 
           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下列:

  1. XPath 的使用translate() http://www.w3.org/TR/xpath#function-translate功能去掉冒号

  2. 使用last() http://www.w3.org/TR/xpath#function-last功能在第二个表达式中

  3. 无需将时间转换为秒比较前

  4. 当用作 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下列:

  1. 在 XPath 2.0 中xs:time http://www.w3.org/TR/xmlschema-2/#time是本机数据类型。然而,为了构建一个xs:time()根据 xml 文档中的值,我们必须将缺少的秒部分连接到它们。
  2. 在 XPath 2.0 中xs:time值可以与“原子值比较运算符 http://www.w3.org/TR/xpath-functions/#comp.duration.datetime"例如lt or gt.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xpath查询和时间 的相关文章

随机推荐