我正在尝试将XML文件转换为第三方软件可以读取的格式。我的问题是如何在每个XML文件中格式化时间。第一个命令的输出格式为hh:mm:ss,但是目标软件会感到困惑,并将其读取为mm:ss。因此,任何事件看起来都比实际短60倍!开发人员建议我需要将小时和分钟转换为分钟。
在模板的早期,在转换摘要数据时,我使用了以下内容:
<xsl:attribute name="duration">
<xsl:value-of select="format-number(floor(dive/summary/calculated/@duration div 60), '00')"/>
<xsl:value-of select="format-number(dive/summary/calculated/@duration mod 60, ':00')"/>
</xsl:attribute>将1:02:50巧妙地转换为62:50。到现在为止还好。当我在for-each元素中使用相同的结构时,事情就不会那么好用了。我的模板的相关部分如下:
<xsl:for-each select="dive/data/row">
<sample>
<xsl:attribute name="duration">
<xsl:value-of select="format-number(floor(@duration div 60), '00')"/>
<xsl:value-of select="format-number(@duration mod 60, ':00')"/>
</xsl:attribute>
<xsl:attribute name="depth"><xsl:value-of select="format-number(@depth, '0.00')"/></xsl:attribute>
<xsl:attribute name="temp"><xsl:value-of select="@temp"/></xsl:attribute>
<xsl:attribute name="po2"><xsl:value-of select="format-number(@po2-av, '0.00')"/></xsl:attribute>
</sample>
</xsl:for-each>下面显示了我的XML输出的相关部分的示例:
<sample duration="NaNNaN" depth="7.30" temp="18" po2="0.65"/>
<sample duration="NaNNaN" depth="7.50" temp="18" po2="0.56"/>
<sample duration="NaNNaN" depth="7.50" temp="18" po2="0.55"/>我还尝试使用一个变量来格式化时间戳,但最终在样式表中出现了编译错误:
<xsl:variable name="dur" select="@duration" />
<xsl:value-of select="format-number(floor($seconds div 60) mod 60, ':00')"/>
<xsl:value-of select="format-number($seconds mod 60, ':00')"/>
<xsl:attribute name="time"><xsl:value-of select="@dur"/></xsl:attribute>首先,有人可以解释为什么数字格式在第一个实例中有效,而不是在for-each元素中有效。其次,我希望能对XLST代码的工作方式有所了解。
编辑:现在包含了示例输入XML:
<dive UID="VMS-RED-5051_#-127#4AB4B4675CCE7036_dive_842960947_3084" profile="1" interface-version="vms-2-5" code-version="3.1.9" record-interval="5" serial-number="4AB4B4675CCE7036" records="755" total-hours=" 47" summary-only="false">
<summary time-start=" 842960947" max-depth=" 30.84" time-end=" 842964718" time-offset="-127" time-datum="1991-12-31 00:00:00">
<tissues ... </tissues>
<gasses> ... </gasses>
<calculated duration="3771" start-date="2018-09-16 11:51:14" average-depth="13.277" average-tpm="12.877"/>
</summary>
<data>
<row index="0" depth="1.1" po2-av="0.64" valve="71" temp="19" filter="68" gas-no="1" bat-2="3.96" bat-1="3.96" hp-dil="219" hp-02="175" po2-cell-1="0.64" po2-cell-2="0.65" po2-cell-3="0.65" setpoint="0.60" tissue-ceiling="0" hud="1" sensor_flags="97" co2="0.00" duration="00:00:00">
<tpm .../>
</row>
<row index="1" depth="1.1" po2-av="0.64" valve="0" temp="19" filter="68" gas-no="1" bat-2="3.96" bat-1="3.96" hp-dil="218" hp-02="175" po2-cell-1="0.64" po2-cell-2="0.65" po2-cell-3="0.65" setpoint="0.60" tissue-ceiling="0" hud="1" sensor_flags="97" co2="0.00" duration="00:00:05">
<tpm .../>
</row>
...
<row index="752" depth="1.7" po2-av="0.89" valve="6" temp="14" filter="37" gas-no="1" bat-2="3.95" bat-1="3.95" hp-dil="168" hp-02="117" po2-cell-1="0.89" po2-cell-2="0.88" po2-cell-3="0.90" setpoint="0.92" tissue-ceiling="0" hud="1" sensor_flags="97" co2="1.25" duration="01:02:40">
<tpm .../>
<alarms>
<alarm> ... </alarms>
</row>
<row index="753" depth="0.9" po2-av="0.87" valve="4" temp="14" filter="37" gas-no="1" bat-2="3.95" bat-1="3.95" hp-dil="168" hp-02="117" po2-cell-1="0.87" po2-cell-2="0.86" po2-cell-3="0.88" setpoint="0.86" tissue-ceiling="0" hud="1" sensor_flags="97" co2="1.27" duration="01:02:45">
<tpm .../>
<alarms>
<alarm> ... </alarms>
</row>
<row index="754" depth="0.2" po2-av="0.77" valve="0" temp="14" filter="37" gas-no="1" bat-2="3.95" bat-1="3.95" hp-dil="168" hp-02="116" po2-cell-1="0.77" po2-cell-2="0.77" po2-cell-3="0.78" setpoint="0.80" tissue-ceiling="0" hud="1" sensor_flags="97" co2="1.27" duration="01:02:50">
<tpm .../>
<alarms>
<alarm> ... </alarms>
</row>
</data>
</dive>发布于 2018-09-26 23:43:44
在calculated元素上,duration属性的值为"3771“,这是一个数字(秒?)
但是,在row元素上,duration属性的值"01:02:40“(例如)不是一个数字,因此您不能对其执行数字计算。
但是,您可以使用一些字符串函数来提取组件,并对其执行数值函数。
例如
<xsl:attribute name="duration">
<xsl:value-of select="format-number(number(substring-before(@duration, ':')) * 60 + number(substring-before(substring-after(@duration, ':'), ':')), '00')"/>
<xsl:value-of select="substring-after(substring-after(@duration, ':'), ':')" />
</xsl:attribute>但是,如果可以使用XSLT2.0,则可以利用format-time使其更具可读性
<xsl:attribute name="duration">
<xsl:value-of select="format-number(xs:integer(format-time(@duration, '[H1]')) * 60 + xs:integer(format-time(@duration, '[m1]')), '00')" />
<xsl:value-of select="format-time(@duration, '[s01]')" />
</xsl:attribute>(其中xs:被声明为名称空间http://www.w3.org/2001/XMLSchema)
发布于 2018-09-26 23:48:26
您正在对@duration属性进行算术运算:
floor(@duration div 60)示例输入中的@duration属性不是数字:
duration="00:00:00"对不是数字的值进行算术运算会得到NaN。
https://stackoverflow.com/questions/52518159
复制相似问题