通过XSL从XML属性将纪元转换为日期并以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.

我每天有一个第三方应用程序向我输出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