我有这个XSL脚本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msa="http://www.publictalksoftware.co.uk/msa">
<xsl:output method="html" indent="yes" version="4.01"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/>
<xsl:template match="/">
<html>
<head>
<title>Publishers Report</title>
<link rel="stylesheet" type="text/css" href="Custom Publisher Report.css"/>
</head>
<body>
<xsl:variable name="PubDB" select="document('MSA_PublisherDatabase.XML')"/>
<table>
<thead>
<th class="cellHeading">Name</th>
<th class="cellHeading">Bible Reading</th>
</thead>
<tbody>
<xsl:apply-templates select="$PubDB/msa:PublisherDatabase/msa:Publishers/msa:Publisher">
<xsl:sort select="msa:Name" data-type="text" order="ascending"/>
</xsl:apply-templates>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="msa:Publisher">
<xsl:variable name="HistoryDB" select="document('AssignHistory.XML')"/>
<xsl:variable name ="SearchName" select="msa:Name"/>
<tr>
<!--The first cell is the name-->
<td>
<xsl:value-of select="msa:Name"/>
</td>
<!--The second cell is the most recent Bible Reading date-->
<!--So we need to locate all "Bible Reading" items for the name in question-->
<!--We sort the entries in date descending order so that the first entry is the most recent-->
<td>
<xsl:for-each select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Description='Bible Reading' and Name=$SearchName]">
<xsl:sort select="$HistoryDB/AssignmentHistory/*" order="descending" data-type="text"/>
<!--We only want the first entry-->
<xsl:if test="position()=1">
<xsl:value-of select="name(../..)"/>
</xsl:if>
</xsl:for-each>
<!--If no entries were found then it ned to write out "<xsl:text> </xsl:text>" How?-->
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
如何检测在for-for中是否没有找到项目?在这种情况下,我要为单元格输入一个空白区域。
我仍然没有解决此问题。
答案 0 :(得分:1)
检查循环是否将被执行的一个选择是找到count()
。可以通过以下方式确定for-each
XPath中符合条件的项目数
<xsl:variable name="numItems" select="count($HistoryDB/AssignmentHistory/*/StudentItems/Item[Description='Bible Reading' and Name=$SearchName])" />
然后使用<xsl:choose>
,<xsl:when>
和<xsl:otherwise>
,可以应用if-else逻辑。
<xsl:choose>
<xsl:when test="$numItems > 0"> <!-- check count > 0 -->
<!-- Perform for loop -->
</xsl:when>
<xsl:otherwise>
<!-- If no entries are found -->
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
所以<td>
看起来像
<td>
<xsl:variable name="numItems" select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Description='Bible Reading' and Name=$SearchName]" />
<xsl:choose>
<xsl:when test="$numItems > 0"> <!-- check count > 0 -->
<xsl:for-each select="$HistoryDB/AssignmentHistory/*/StudentItems/Item[Description='Bible Reading' and Name=$SearchName]">
<xsl:sort select="$HistoryDB/AssignmentHistory/*" order="descending" data-type="text" />
<!--We only want the first entry -->
<xsl:if test="position() = 1">
<xsl:value-of select="name(../..)" />
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<!-- If no entries are found, add space -->
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</td>