通过 XSL 从 XML 属性将纪元转换为日期并以 HTML 显示 [英] Convert epoch to Date via XSL from XML attribute and display in HTML
问题描述
第一次发帖,对使用 XML 和 XSL 非常陌生.
First time posting and am very new to working with XML and XSL.
我在这个板上花了两天时间,其他人也在寻找我的答案.我看到的帖子与我的相似,但不准确.如果这是多余的,我很抱歉.
I've spent two days on this board and others looking for my answer. I see posts similar to mine, but not exact. My apologies if this is redundant.
我每天都有一个来自第 3 方应用程序的 XML 文档输出.我需要在网页上显示其中的两条信息:LoginName 和 LastBackupDate.
I have an XML document output to me from a 3rd party application on a daily basis. I need to display on a webpage two pieces of information from it: LoginName and LastBackupDate.
我可以通过我编写的 XSL 来做到这一点.但是,LastBackupDate 采用纪元格式.我需要将其转换为人类可读的日期/时间 (mm-dd-yyyy hh:mm:ss).
I am able to do this via an XSL that I wrote. However, the LastBackupDate is in epoch format. I need to convert it to human readable Date/Time (mm-dd-yyyy hh:mm:ss).
是否可以通过 XSL 样式表即时"转换它?
Is it possible to convert this "on the fly" via an XSL stylesheet?
如果是这样,有人可以帮忙吗?我已经尝试了很多我在这里和其他几个网站上发现的变化,我现在不知所措......
If so, can somebody assist? I've tried so many variations of what I've found here and on several other websites that I'm now at a loss...
对我来说绝对让这更困难的是 XML 文件的格式 - 它主要是属性.我发现的大多数示例都使用元素.
What is definitely making this more difficult for me is the format of the XML file - it is mostly attributes. Most every example I find uses elements.
这是我的 XML (company1.xml):
Here is my XML (company1.xml):
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="company1.xsl" ?>
<Users>
<User LoginName="server1" Owner="company1" UserId="server1#24545" Alias="server1" UserType="PAID" ClientType="ERM" Quota="536870912000" Timezone="GMT+01:00 (CEST)" Language="en" DataFile="191277" DataSize="120105299488" RetainFile="1195" RetainSize="49220308" EnableMSSQL="Y" EnableMSExchange="N" MsExchangeQuota="0" EnableOracle="N" EnableLotusNotes="N" EnableLotusDomino="N" EnableMySQL="N" EnableInFileDelta="Y" EnableShadowCopy="Y" EnableExchangeMailbox="N" ExchangeMailboxQuota="0" EnableNASClient="N" EnableDeltaMerge="N" EnableMsVm="N" MsVmQuota="0" EnableVMware="N" VMwareQuota="0" Bandwidth="0" Notes="" Status="ENABLE" RegistrationDate="1222175386552" SuspendPaidUser="N" SuspendPaidUserDate="20110221" LastBackupDate="1421247632689" EnableCDP="Y" EnableShadowProtectBareMetal="Y" EnableWinServer2008BareMetal="Y" Hostname="backup.company1.com">
<Contact Name="Spain Reception" Email="spain.reception@company1.com" />
</User>
<User LoginName="server2" Owner="company1" UserId="server2#24545" Alias="server2" UserType="PAID" ClientType="ERM" Quota="536870912000" Timezone="GMT-06:00 (CDT)" Language="en" DataFile="123920" DataSize="69665584875" RetainFile="0" RetainSize="0" EnableMSSQL="Y" EnableMSExchange="N" MsExchangeQuota="0" EnableOracle="N" EnableLotusNotes="N" EnableLotusDomino="N" EnableMySQL="N" EnableInFileDelta="Y" EnableShadowCopy="Y" EnableExchangeMailbox="N" ExchangeMailboxQuota="0" EnableNASClient="N" EnableDeltaMerge="N" EnableMsVm="N" MsVmQuota="0" EnableVMware="N" VMwareQuota="0" Bandwidth="0" Notes="" Status="ENABLE" RegistrationDate="1212767489088" SuspendPaidUser="N" SuspendPaidUserDate="20141223" LastBackupDate="1361926839272" EnableCDP="Y" EnableShadowProtectBareMetal="Y" EnableWinServer2008BareMetal="Y" Hostname="backup.company1.com">
<Contact Name="server2" Email="IT.systemadministrators@company.com" />
</User>
</Users>
这是我的 XSL (company1.xsl):
Here is my XSL (company1.xsl):
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h3>COMPANY1 Last Backup Date</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>LoginName</th>
<th>Epoch LastBackupDate</th>
<th>Human dateTime</th>
</tr>
<xsl:for-each select="//User">
<tr>
<td>
<xsl:value-of select="@LoginName" />
</td>
<td>
<xsl:value-of select="@LastBackupDate" />
</td>
<td>
<xsl:value-of select='xs:dateTime("1970-01-01T00:00:00") @LastBackupDate * xs:dayTimeDuration("PT0.001S")'/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
推荐答案
没有纪元格式"这样的东西.AFAICT,您的 LastBackupDate 实际上是自 1970-01-01T00:00:00 以来经过的毫秒数.在 XSLT 1.0 中,您可以使用以下模板将其转换为 ISO 日期时间表示:
There is no such thing as an "epoch format". AFAICT, your LastBackupDate is actually the number of milliseconds elapsed since 1970-01-01T00:00:00. In XSLT 1.0, you can use the following template to convert it to ISO date-time representation:
<xsl:template name="millisecs-to-ISO">
<xsl:param name="millisecs"/>
<xsl:param name="JDN" select="floor($millisecs div 86400000) + 2440588"/>
<xsl:param name="mSec" select="$millisecs mod 86400000"/>
<xsl:param name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
<xsl:param name="e" select="4*$f + 3"/>
<xsl:param name="g" select="floor(($e mod 1461) div 4)"/>
<xsl:param name="h" select="5*$g + 2"/>
<xsl:param name="d" select="floor(($h mod 153) div 5 ) + 1"/>
<xsl:param name="m" select="(floor($h div 153) + 2) mod 12 + 1"/>
<xsl:param name="y" select="floor($e div 1461) - 4716 + floor((14 - $m) div 12)"/>
<xsl:param name="H" select="floor($mSec div 3600000)"/>
<xsl:param name="M" select="floor($mSec mod 3600000 div 60000)"/>
<xsl:param name="S" select="$mSec mod 60000 div 1000"/>
<xsl:value-of select="concat($y, format-number($m, '-00'), format-number($d, '-00'))" />
<xsl:value-of select="concat(format-number($H, 'T00'), format-number($M, ':00'), format-number($S, ':00'))" />
</xsl:template>
调用示例:
<xsl:call-template name="millisecs-to-ISO">
<xsl:with-param name="millisecs" select="1421247632689" />
</xsl:call-template>
结果:
2015-01-14T15:00:33
这篇关于通过 XSL 从 XML 属性将纪元转换为日期并以 HTML 显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!