[英]Xslt template. xsl if not working
我正在嘗試創建2個表,其中一個顯示何時更大,另一個顯示何時更少。 第一個表工作正常,但是創建第二個表卻行不通。
<?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<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>=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>
我創建了一個if語句,但僅適用於第一個表。 我可以做一個select語句來創建表和測試元素嗎?
在match="year"
模板中,使用條件test="year>=2005"
。 XPath表達式中的裸名year
表示./child::year
,即選擇上下文節點的year
子級。 但是上下文節點是year
元素,我懷疑它有一個名為year
的孩子。 使用test=".>=2005"
。
我懷疑year
元素也沒有稱為title
或Director
。 我懷疑您真正想要的是擁有一個具有match="movie[year>=2005]"
模板規則,以及一個具有match="movie[year<2005]"
模板規則。
但是,由於您沒有顯示輸入或預期輸出,因此很難確切知道您要實現的目標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.