Fun*_*nky 159 xml xslt if-statement
我试图在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>
Run Code Online (Sandbox Code Playgroud)
px1*_*1mp 299
你必须使用<xsl:choose>标签重新实现它:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:when>
<xsl:otherwise>
<h2> dooooooooooooo </h2>
</xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)
Inf*_*nd' 61
If语句用于快速检查一个条件.如果您有多个选项,请使用<xsl:choose>如下图所示:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2>mooooooooooooo</h2>
</xsl:when>
<xsl:otherwise>
<h2>dooooooooooooo</h2>
</xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)
此外,您可以使用多个<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>
Run Code Online (Sandbox Code Playgroud)
前面的示例等同于下面的伪代码:
if ($CreatedDate > $IDAppendedDate)
{
output: <h2>mooooooooooooo</h2>
}
else if ($CreatedDate = $IDAppendedDate)
{
output: <h2>booooooooooooo</h2>
}
else
{
output: <h2>dooooooooooooo</h2>
}
Run Code Online (Sandbox Code Playgroud)
kjh*_*hes 36
如果我可以提出一些建议(两年后,但希望对未来的读者有所帮助):
h2因素.ooooooooooooo文本排除在外.if/then/else如果使用XSLT 2.0,请注意新的XPath 2.0 构造.<h2>
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">m</xsl:when>
<xsl:otherwise>d</xsl:otherwise>
</xsl:choose>
ooooooooooooo
</h2>
Run Code Online (Sandbox Code Playgroud)
<h2>
<xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
ooooooooooooo
</h2>
Run Code Online (Sandbox Code Playgroud)