简体   繁体   中英

Xslt template. xsl if not working

I'm trying to create 2 tables one that display when is greater and one for when is less. The first table is working fine but creating a second table is not working.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>
  <!-- template that is use for root-->
  <xsl:template match="/">

    <html>
      <!--Displays a heading above the list of movie title-->
      <head>
        <title> Movies</title>
      </head>
      <body>
        <!--Displays a heading above the list of movie title-->
        <h1 align="center">
          <font color="red" size="10">Movies  Listing</font>
        </h1>
        <!-- Creates the table-->
        <table style="color:blue;" bgcolor="gray" cellpadding="5" border="1" align="center">
          <tr style="font-family:arial; color:blue;">
            <td>Movie ID</td>
            <td>Title</td>
            <td>Director</td>
            <td> Movie Year</td>
          </tr>
          <!-- apply template stament to use the template for movie-->
          <xsl:apply-templates select="movies/movie">
            <xsl:sort select="title" order="ascending"></xsl:sort>
          </xsl:apply-templates>
        </table>

      </body>
    </html>
  </xsl:template>

  <!-- template for each movie element-->

  <xsl:template match="movie">
    <xsl:if test="year&lt;2005">
    <tr>
      <td>
        <xsl:apply-templates select="@id"/>
      </td>
      <td>
        <xsl:apply-templates select="title"/>
      </td>
      <td>
        <xsl:apply-templates select="Director"/>
      </td>
      <td>
        <xsl:apply-templates select="year"/>
      </td>
    </tr>
    </xsl:if>
    <xsl:apply-templates select="year">
  </xsl:template>


  <xsl:template match="year">
    <table>
      <tr style="font-family:arial; color:blue;">
        <td>Movie ID</td>
        <td>Title</td>
        <td>Director</td>
        <td> Movie Year</td>
      </tr>
      <xsl:if test="year&gt;=2005">
        <tr>
          <td>
            <xsl:apply-templates select="@id"/>
          </td>
          <td>
            <xsl:apply-templates select="title"/>
          </td>
          <td>
            <xsl:apply-templates select="Director"/>
          </td>
          <td>
            <xsl:apply-templates select="year"/>
          </td>
        </tr>
      </xsl:if>  
    </table>
  </xsl:template>
  <!-- template for color and font use for text in each element-->
  <xsl:template match="@id">
    <span style="font-family:arial; color:blue;">
      <xsl:value-of select="."/>
    </span>
  </xsl:template>

  <xsl:template match="title">
    <span style="font-family:arial; color:blue;">
      <xsl:value-of select="."/>
    </span>
  </xsl:template>

  <xsl:template match="Director">
    <span style="font-family:arial; color:blue;">
      <xsl:value-of select="."/>
    </span>
  </xsl:template>

  <xsl:template match="year">
    <span style="font-family:arial; color:blue;">
      <xsl:value-of select="."/>
    </span>
  </xsl:template>

</xsl:stylesheet>

I created an if statement but is only working for the first table. Can I do a choose statement to create both table and test the element?

In the match="year" template, you use the condition test="year>=2005" . A bare name year in an XPath expression means ./child::year , that is, select the year children of the context node. But the context node is a year element, and I doubt it has a child called year . Use test=".>=2005" .

I suspect that the year element doesn't have children called title or Director either. I suspect what you really want is to have one template rule with match="movie[year>=2005]" and one with match="movie[year<2005]" .

But since you haven't shown your input or expected output, it's hard to know exactly what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM