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>
4 个解决方案
#1
240
You have to reimplement it using <xsl:choose>
tag:
您必须使用
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:when>
<xsl:otherwise>
<h2> dooooooooooooo </h2>
</xsl:otherwise>
</xsl:choose>
#2
51
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: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 If
or Switch
patterns as illustrated below:
此外,您可以使用多个
<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>
}
#3
30
If I may offer some suggestions (two years later but hopefully helpful to future readers):
如果我能提供一些建议(两年后希望对未来的读者有所帮助):
- Factor out the common
h2
element. - 提出共同的h2元素。
- Factor out the common
ooooooooooooo
text. - 提出常见的ooooooooooooo文本。
- Be aware of new XPath 2.0
if/then/else
construct if using XSLT 2.0. - 如果使用XSLT 2.0,请注意新的XPath 2.0 if/then/else结构。
XSLT 1.0 Solution (also works with 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
<h2>
<xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
ooooooooooooo
</h2>
#4
2
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-test,但条件相反。这种技术更短,更容易看,也更容易得到正确,而不是选择什么时候嵌套的块:
<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。
#1
240
You have to reimplement it using <xsl:choose>
tag:
您必须使用
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:when>
<xsl:otherwise>
<h2> dooooooooooooo </h2>
</xsl:otherwise>
</xsl:choose>
#2
51
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: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 If
or Switch
patterns as illustrated below:
此外,您可以使用多个
<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>
}
#3
30
If I may offer some suggestions (two years later but hopefully helpful to future readers):
如果我能提供一些建议(两年后希望对未来的读者有所帮助):
- Factor out the common
h2
element. - 提出共同的h2元素。
- Factor out the common
ooooooooooooo
text. - 提出常见的ooooooooooooo文本。
- Be aware of new XPath 2.0
if/then/else
construct if using XSLT 2.0. - 如果使用XSLT 2.0,请注意新的XPath 2.0 if/then/else结构。
XSLT 1.0 Solution (also works with 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
<h2>
<xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
ooooooooooooo
</h2>
#4
2
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-test,但条件相反。这种技术更短,更容易看,也更容易得到正确,而不是选择什么时候嵌套的块:
<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。