从xs:date中提取年份

时间:2014-10-15 02:04:47

标签: xml xslt

我想在我的输出中只显示21世纪的总统。我将如何在xsl文档中进行此操作?我是否需要在xml文件中使用命名空间并将其合并到.xsd和xsl文件中?如何从xs:date值中提取日期?

这是我的xml:

<?xml-stylesheet type="text/xsl" href="president_21c.xsl"?>

<presidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="president.xsd" xmlns:president="http://www.du.edu/~mschwart/xml/president" date="2014-09-24">

    <president>
        <number>41</number>
        <name>George H. W. Bush</name>
         <birthday>1924-06-12</birthday>
        <took_office>1989-01-20</took_office>
        <left_office>1993-01-20</left_office>
        <party>Republican</party>
        <term>
            <term_number>51</term_number>
            <vice_president>Dan Quayle</vice_president>
        </term>
         <president_image>images/41.jpg</president_image>
    </president>

    <president>
        <number>42</number>
        <name>Bill Clinton</name>
         <birthday>1946-08-19</birthday>
        <took_office>1993-01-20</took_office>
        <left_office>2001-01-20</left_office>
        <party>Democratic</party>
        <term>
            <term_number>52</term_number>
            <vice_president>Al Gore</vice_president>
        </term>
        <term>
            <term_number>53</term_number>
            <vice_president>Al Gore</vice_president>
        </term>
         <president_image>images/42.jpg</president_image>
    </president>

    <president>
        <number>43</number>
        <name>George W. Bush</name>
         <birthday>1946-07-06</birthday>
        <took_office>2001-01-20</took_office>
        <left_office>2009-01-20</left_office>
        <party>Republican</party>
        <term>
            <term_number>54</term_number>
            <vice_president>Dick Cheney</vice_president>
        </term>
        <term>
            <term_number>55</term_number>
            <vice_president>Dick Cheney</vice_president>
        </term>
         <president_image>images/43.jpg</president_image>
    </president>

    <president>
        <number>44</number>
        <name>Barack Obama</name>
         <birthday>1961-08-04</birthday>
        <took_office>2009-01-20</took_office>
        <left_office xsi:nil="true"/>
        <party>Democratic</party>
        <term>
            <term_number>56</term_number>
            <vice_president>Joe Biden</vice_president>
        </term>
         <president_image>images/44.jpg</president_image>
    </president>
</presidents>

和我的xsl:

<?xml version="1.0"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" version="4.0"/>

<xsl:template match="/">
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="president_21c.css"/>
    <title>Table of Us Presidents</title>
    </head>
    <body>
        <h1>Table of Us Presidents</h1>
        <table>
            <tr>
                <th>Name</th>
                <th>Birthday</th>
                <th>Took Office</th>
                <th>Left Office</th>
                <th>Party</th>
                <th>Vice President</th>
                <th>Portrait</th>
            </tr>
            <xsl:apply-templates select="//president">
                <xsl:sort select="party"/>
            </xsl:apply-templates>
        </table>
    </body>
    </html>
</xsl:template>

<xsl:template match="president">
<tr>
    <td><xsl:apply-templates select="name"/></td>
    <td><xsl:apply-templates select="birthday"/></td>
    <td><xsl:apply-templates select="took_office"/></td>
    <td><xsl:apply-templates select="left_office"/></td>
    <td><xsl:apply-templates select="party"/></td>
    <td>
    <xsl:for-each select="term">
        <xsl:number value="position()" format="1. " />
        <xsl:value-of select="vice_president" /><br />
    </xsl:for-each>
    </td>
    <td align="center" style="background-color: #E1E0E0;">
    <img src="{president_image}" class="president_image"/>
    </td>
</tr>
</xsl:template>



</xsl:stylesheet> 

2 个答案:

答案 0 :(得分:1)

从日期中提取年份的正式方法是使用year-from-date() function

非正式方法(通常更方便)是使用:

substring-before(your-date, '-')

答案 1 :(得分:0)

对于那些想知道如何使用 year-from-date 的人:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"   
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:output method="html" version="4.0"/>
  
  <xsl:template match="/">
    <html>
      <head>
        <link rel="stylesheet" type="text/css" href="president_21c.css"/>
        <title>Table of Us Presidents</title>
      </head>
      <body>
        <h1>Table of Us Presidents</h1>
        <table>
          <tr>
            <th>Name</th>
            <th>Birthday</th>
            <th>Took Office</th>
            <th>Left Office</th>
            <th>Party</th>
            <th>Vice President</th>
            <th>Portrait</th>
          </tr>
          <xsl:apply-templates select="presidents/president[left_office[@xsi:nil='true' or year-from-date(xs:date(.)) ge 2000 ]]">
            <xsl:sort select="party"/>
          </xsl:apply-templates>
        </table>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="president">
    <tr>
      <td><xsl:apply-templates select="name"/></td>
      <td><xsl:apply-templates select="birthday"/></td>
      <td><xsl:apply-templates select="took_office"/></td>
      <td><xsl:apply-templates select="left_office"/></td>
      <td><xsl:apply-templates select="party"/></td>
      <td>
        <xsl:for-each select="term">
          <xsl:number value="position()" format="1. " />
          <xsl:value-of select="vice_president" /><br />
        </xsl:for-each>
      </td>
      <td align="center" style="background-color: #E1E0E0;">
        <img src="{president_image}" class="president_image"/>
      </td>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>