xml 如何在 XSLT 中实现 if-else 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13622338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement if-else statement in XSLT?
提问by Funky
I am trying to implement an if -else statement in XSLT but my code just doesn't parse. Does anyone have any ideas?
我正在尝试在 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>
回答by px1mp
You have to reimplement it using <xsl:choose>tag:
您必须使用<xsl:choose>标签重新实现它:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:when>
<xsl:otherwise>
<h2> dooooooooooooo </h2>
</xsl:otherwise>
</xsl:choose>
回答by InfantPro'Aravind'
If statement is used for checking just one condition quickly.
When you have multiple options, use <xsl:choose>as illustrated below:
If 语句用于快速检查一个条件。当您有多个选项时,请<xsl:choose>按如下所示使用:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2>mooooooooooooo</h2>
</xsl:when>
<xsl:otherwise>
<h2>dooooooooooooo</h2>
</xsl:otherwise>
</xsl:choose>
Also, you can use multiple <xsl:when>tags to express If .. Else Ifor Switchpatterns as illustrated below:
此外,您可以使用多个<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>
The previous example would be equivalent to the pseudocode below:
前面的例子相当于下面的伪代码:
if ($CreatedDate > $IDAppendedDate)
{
output: <h2>mooooooooooooo</h2>
}
else if ($CreatedDate = $IDAppendedDate)
{
output: <h2>booooooooooooo</h2>
}
else
{
output: <h2>dooooooooooooo</h2>
}
回答by kjhughes
If I may offer some suggestions (two years later but hopefully helpful to future readers):
如果我可以提供一些建议(两年后,但希望对未来的读者有所帮助):
- Factor out the common
h2element. - Factor out the common
oooooooooooootext. - Be aware of new XPath 2.0
if/then/elseconstruct if using XSLT 2.0.
- 分解出共同
h2元素。 - 分解出常见的
ooooooooooooo文本。 if/then/else如果使用 XSLT 2.0,请注意新的 XPath 2.0构造。
XSLT 1.0 Solution (also works with XSLT 2.0)
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 Solution
XSLT 2.0 解决方案
<h2>
<xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
ooooooooooooo
</h2>
回答by Raymond Hettinger
The most straight-forward approach is to do a second if-test but with the condition inverted. This technique is shorter, easier on the eyes, and easier to get right than a choose-when-otherwise nested block:
最直接的方法是进行第二次 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>
Here's a real-world example of the technique being used in the style-sheet for a government website: http://w1.weather.gov/xml/current_obs/latest_ob.xsl
这是政府网站样式表中使用的技术的真实示例:http: //w1.weather.gov/xml/current_obs/latest_ob.xsl
回答by AabinGunz
Originally from this blog post. We can achieve if else by using below code
最初来自这篇博文。我们可以通过使用下面的代码来实现 if else
<xsl:choose>
<xsl:when test="something to test">
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
So here is what I did
所以这就是我所做的
<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>
My Output
我的输出



