如何在xsl中添加jquery函数(Date)

问题描述 投票:0回答:1

我得到日期作为字符串,需要以这样的格式显示日期:MM / DD / YYYY如果只有一年我应该添加01/01。我还需要用大胆的方式显示今天2年以上的专辑。我该怎么做呢?如何添加功能?我只能使用JQUERY而不能使用JS。谢谢。

这是我的xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:ms="urn:schemas-microsoft-com:xslt"
      xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <xsl:template match="/">
<html> 
<body>
<style>
u { 
    text-decoration: underline;
    font-weight: bold;
}
</style>
  <table border="1">
    <tr bgcolor="#979994">
      <th style="text-align:left">Price</th>
      <th style="text-align:left">Link</th>
      <th style="text-align:left">Company</th>
      <th style="text-align:left">Name</th>
      <th style="text-align:left">Date</th>
      <th style="text-align:left">Artist</th>
    </tr>
    <xsl:for-each select="Albums/Album">
    <tr>
      <td><xsl:value-of select="Price"/></td>
      <td><xsl:value-of select="Link"/></td>
      <td><xsl:value-of select="Company"/></td>
      <td><xsl:value-of select="Name"/></td>
      <td><xsl:value-of select="Date" /></td>
      <td style="color:red;"><xsl:value-of select="Artist"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

和xml:

<?xml version="1.0" encoding="ISO8859-1"?>
<?xml-stylesheet type="text/xsl" href="Albums1.xs"?>
<Albums>
    <Album>
        <Name>Empire Burlesque</Name>
        <Artist>Bob Dylan</Artist>
        <Country>USA</Country>
        <Company>Columbia</Company>
        <Date>19880610</Date>
    </Album>
    <Album>
        <Name>Hide your heart</Name>
        <Artist>Bonnie Tylor</Artist>
        <Country>UK</Country>
        <Company>CBS Records</Company>
        <Price>9.90</Price>
        <Date>19880509</Date>
    </Album>
    <Album>
        <Name>Greatest Hits</Name>
        <Artist>Dolly Parton</Artist>
        <Country>USA</Country>
        <Company>RCA</Company>
        <Price>9.90</Price>
        <Date>1982</Date>
    </Album>
    <Album>
        <Name>Still got the blues</Name>
        <Artist>Gary More</Artist>
        <Country>UK</Country>
        <Company>Virgin redords</Company>
        <Price>10.20</Price>
        <Date>1990</Date>
    </Album>
    <Album>
        <Name>Eros</Name>
        <Artist>Eros Ramazzotti</Artist>
        <Country>EU</Country>
        <Company>BMG</Company>
        <Price>9.90</Price>
        <Date>1997</Date>
    </Album>
    <Album>
        <Name>25</Name>
        <Artist>Adele</Artist>
        <Country>UK</Country>
        <Company>XL Recordings</Company>
        <Price>9.90</Price>
        <Date>20151120</Date>
    </Album>
    <Album>
        <Name>1000 Forms of Fear</Name>
        <Artist>Sia</Artist>
        <Country>USA</Country>
        <Company>RCA Records</Company>
        <Price>9.90</Price>
        <Date>20140704</Date>
    </Album>
    <Album>
        <Name>Rattle and Hum</Name>
        <Artist>U2</Artist>
        <Country>EU</Country>
        <Company>Island</Company>
        <Price>9.90</Price>
        <Date>19881010</Date>
    </Album>
</Albums>
javascript jquery xml date xslt
1个回答
0
投票

虽然可以在XSLT中调用javascript函数,但它依赖于处理器。但是,你根本不需要javascript。这种简单的格式是XSLT可以直接处理的。

而不是这样做......

<td><xsl:value-of select="Date" /></td>

做这个....

  <td>
    <xsl:choose>
      <xsl:when test="string-length(Date) = 4">
        <xsl:text>01/01/</xsl:text>
        <xsl:value-of select="Date" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="substring(Date, 7, 2)" />
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(Date, 5, 2)" />
        <xsl:text>/</xsl:text>
        <xsl:value-of select="substring(Date, 1, 4)" />
      </xsl:otherwise>
    </xsl:choose>
  </td>

或许这......

  <td>
    <xsl:choose>
      <xsl:when test="string-length(Date) = 4">
        <xsl:value-of select="concat('01/01/', Date)" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat(substring(Date, 7, 2), '/', substring(Date, 5, 2), '/', substring(Date, 1, 4))" />
      </xsl:otherwise>
    </xsl:choose>
  </td>
© www.soinside.com 2019 - 2024. All rights reserved.