Convert epoch to Date via XSL from XML attribute and display in HTML

房东的猫 提交于 2019-11-27 07:19:41

问题


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.

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.

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

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

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.

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>

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>

回答1:


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> 

Example of call:

<xsl:call-template name="millisecs-to-ISO">
    <xsl:with-param name="millisecs" select="1421247632689" />
</xsl:call-template>

Result:

2015-01-14T15:00:33



回答2:


Here is the simple solution using Java Extension functions

<xsl:value-of select="string(i:ofEpochMilli($millis))" xmlns:i="java:java.time.Instant"/>


来源:https://stackoverflow.com/questions/27990478/convert-epoch-to-date-via-xsl-from-xml-attribute-and-display-in-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!