我正在尝试在 XSLT 中实现 if -else 语句,但我的代码无法解析。有没有人有任何想法?
<xsl:variable name="CreatedDate" select="@createDate"/>
<xsl:variable name="IDAppendedDate" select="2012-01-01" />
<b>date: <xsl:value-of select="$CreatedDate"/></b>
<xsl:if test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:if>
<xsl:else>
<h2> dooooooooooooo </h2>
</xsl:else>
您必须使用 <xsl:choose>
标记重新实现它:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:when>
<xsl:otherwise>
<h2> dooooooooooooo </h2>
</xsl:otherwise>
</xsl:choose>
if 语句用于快速检查一个条件。当您有多个选项时,请使用 <xsl:choose>
,如下图所示:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2>mooooooooooooo</h2>
</xsl:when>
<xsl:otherwise>
<h2>dooooooooooooo</h2>
</xsl:otherwise>
</xsl:choose>
此外,您可以使用多个 <xsl:when>
标记来表达 If .. Else If
或 Switch
模式,如下所示:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2>mooooooooooooo</h2>
</xsl:when>
<xsl:when test="$CreatedDate = $IDAppendedDate">
<h2>booooooooooooo</h2>
</xsl:when>
<xsl:otherwise>
<h2>dooooooooooooo</h2>
</xsl:otherwise>
</xsl:choose>
前面的例子等价于下面的伪代码:
if ($CreatedDate > $IDAppendedDate)
{
output: <h2>mooooooooooooo</h2>
}
else if ($CreatedDate = $IDAppendedDate)
{
output: <h2>booooooooooooo</h2>
}
else
{
output: <h2>dooooooooooooo</h2>
}
如果我可以提供一些建议(两年后但希望对未来的读者有所帮助):
分解出常见的 h2 元素。
分解出常见的 oooooooooooooo 文本。
如果使用 XSLT 2.0,请注意新的 XPath 2.0 if/then/else 构造。
XSLT 1.0 解决方案(也适用于 XSLT 2.0)
<h2>
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">m</xsl:when>
<xsl:otherwise>d</xsl:otherwise>
</xsl:choose>
ooooooooooooo
</h2>
XSLT 2.0 解决方案
<h2>
<xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
ooooooooooooo
</h2>
最初来自这个blog post。我们可以通过使用下面的代码来实现 if else
<xsl:choose>
<xsl:when test="something to test">
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
所以这就是我所做的
<h3>System</h3>
<xsl:choose>
<xsl:when test="autoIncludeSystem/autoincludesystem_info/@mdate"> <!-- if attribute exists-->
<p>
<dd><table border="1">
<tbody>
<tr>
<th>File Name</th>
<th>File Size</th>
<th>Date</th>
<th>Time</th>
<th>AM/PM</th>
</tr>
<xsl:for-each select="autoIncludeSystem/autoincludesystem_info">
<tr>
<td valign="top" ><xsl:value-of select="@filename"/></td>
<td valign="top" ><xsl:value-of select="@filesize"/></td>
<td valign="top" ><xsl:value-of select="@mdate"/></td>
<td valign="top" ><xsl:value-of select="@mtime"/></td>
<td valign="top" ><xsl:value-of select="@ampm"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</dd>
</p>
</xsl:when>
<xsl:otherwise> <!-- if attribute does not exists -->
<dd><pre>
<xsl:value-of select="autoIncludeSystem"/><br/>
</pre></dd> <br/>
</xsl:otherwise>
</xsl:choose>
我的输出
https://i.stack.imgur.com/TokjR.png
最直接的方法是进行第二次 if 测试,但条件相反。这种技术比选择时嵌套块更短,更容易看,也更容易正确:
<xsl:variable name="CreatedDate" select="@createDate"/>
<xsl:variable name="IDAppendedDate" select="2012-01-01" />
<b>date: <xsl:value-of select="$CreatedDate"/></b>
<xsl:if test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:if>
<xsl:if test="$CreatedDate <= $IDAppendedDate">
<h2> dooooooooooooo </h2>
</xsl:if>
以下是政府网站样式表中使用的技术的真实示例:http://w1.weather.gov/xml/current_obs/latest_ob.xsl
if
测试与第一个测试的补码相匹配,这会使任何后续修改更容易出错。
<xsl:choose>
会更直接,其含义也更清晰。
不定期副业成功案例分享
if else
条件只是一个示例,或者是一个伪代码。好吧,我考虑到你的担忧,我已经编辑了它..