Jak zaimplementować instrukcję if-else w XSLT?
Próbuję zaimplementować instrukcję if-else w XSLT, ale mój kod po prostu nie parsuje. Czy ktoś ma jakieś pomysły?
<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 answers
Musisz go ponownie zaimplementować używając <xsl:choose>
tagu:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2> mooooooooooooo </h2>
</xsl:when>
<xsl:otherwise>
<h2> dooooooooooooo </h2>
</xsl:otherwise>
</xsl:choose>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-29 09:25:48
Jeśli polecenie służy do szybkiego sprawdzenia tylko jednego warunku.
Jeśli masz wiele opcji, Użyj <xsl:choose>
, Jak pokazano poniżej:
<xsl:choose>
<xsl:when test="$CreatedDate > $IDAppendedDate">
<h2>mooooooooooooo</h2>
</xsl:when>
<xsl:otherwise>
<h2>dooooooooooooo</h2>
</xsl:otherwise>
</xsl:choose>
Możesz również użyć wielu znaczników <xsl:when>
do wyrażania wzorów If .. Else If
lub Switch
, Jak pokazano poniżej:
<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>
Poprzedni przykład byłby odpowiednikiem poniższego pseudokodu:
if ($CreatedDate > $IDAppendedDate)
{
output: <h2>mooooooooooooo</h2>
}
else if ($CreatedDate = $IDAppendedDate)
{
output: <h2>booooooooooooo</h2>
}
else
{
output: <h2>dooooooooooooo</h2>
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-11 19:06:51
Jeśli mogę zaproponować kilka sugestii (dwa lata później, ale mam nadzieję, że pomocne dla przyszłych czytelników):
- Factor out the common
h2
element. - Oblicz wspólny
ooooooooooooo
tekst. - Bądź świadomy nowej konstrukcji XPath 2.0
if/then/else
, Jeśli używasz XSLT 2.0.
XSLT 1.0 rozwiązanie (współpracuje również z 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 roztwór
<h2>
<xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
ooooooooooooo
</h2>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-21 16:19:12
Najbardziej prostym podejściem jest wykonanie drugiego testu if, ale z odwróconym warunkiem. Technika ta jest krótsza, łatwiejsza dla oczu i łatwiejsza do poprawienia niż zagnieżdżony blok choose-when-otherwise: {]}
<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>
Oto prawdziwy przykład techniki używanej w arkuszu stylów dla rządowej strony internetowej: http://w1.weather.gov/xml/current_obs/latest_ob.xsl
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-14 03:23:39