我正在尝试根据 cooking 内容动态创建一个附加元素 时间元素。我还按 cooking 时间和菜谱名称对菜谱数据进行排序,以显示其他元素。
如果 cooking 时间超过六十分钟,新元素应显示“Slow Burner”字样。如果 cooking 时间小于或等于六十分钟,并且大于或等于三十分钟,则此新元素应显示“Medium Burner”字样。否则,新元素应显示“Quick and Easy”字样。
我收到错误,但我不明白为什么 if 语句不起作用。
为什么?
XSL 在这里:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="about:legacy-compat" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/collection">
<html>
</html>
最佳答案
您可以将循环更改为以下内容。 recipe
元素的位置已更改为包含在相应的 table
中(当然,如果需要,可以将其更改回来)。
<xsl:for-each select="recipe">
<xsl:sort select="cooking_Time" />
<xsl:variable name="time" select="number(replace(cooking_Time,'minutes',''))" />
<tr>
<td>
<h4>
<xsl:value-of select="title" />
</h4>
</td>
<td>
<recipe>
<xsl:value-of select="if ($time > 60) then 'Slow Burner' else if ($time >= 30) then 'Medium Burner' else 'Quick and Easy'" />
</recipe>
</td>
<td>
<xsl:value-of select="description" />
</td>
<td>
<xsl:value-of select="servings" />
</td>
<td>
<xsl:value-of select="preparetion_Time" />
</td>
<td>
<xsl:value-of select="cooking_Time" />
</td>
<td>
<xsl:value-of select="passiveTime" />
</td>
<td>
<xsl:value-of select="difficulty" />
</td>
<td>
<xsl:for-each select="ingredients">
<xsl:for-each select="ingredient">
<li>
<xsl:value-of select="." />
</li>
</xsl:for-each>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
这按 cooking_Time
对元素进行排序(您将 xsl:sort
放在了错误的位置;它必须立即跟在 xsl:for-each
)。
主要逻辑在这个表达式中:
<xsl:value-of select="if ($time > 60) then 'Slow Burner' else if ($time >= 30) then 'Medium Burner' else 'Quick and Easy'" />
它根据 $time
变量(创建该变量是为了简化表达式)的值输出适当的字符串。变量中的 fn:replace
<xsl:variable name="time" select="number(replace(cooking_Time,'minutes',''))" />
注意$time
变量的值始终是数字并且不包含字符串“分钟”。
如果您无法使用 XSLT-2.0,您也可以使用此 XSLT-1.0 解决方案:
<xsl:for-each select="recipe">
<xsl:sort select="normalize-space(cooking_Time)" />
<xsl:variable name="time">
<xsl:choose>
<xsl:when test="contains(cooking_Time,'minutes')">
<xsl:value-of select="number(substring-before(cooking_Time,'minutes'))" />
</xsl:when>
<xsl:when test="number(cooking_Time)">
<xsl:value-of select="number(cooking_Time)" />
</xsl:when>
<xsl:otherwise>
<!-- This value represents any items that don't have a 'cooking_Time' value present -->
<xsl:value-of select="0" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr>
<td>
<h4>
<xsl:value-of select="title" />
</h4>
</td>
<td>
<recipe>
<xsl:choose>
<xsl:when test="$time > 60">
<xsl:text>Slow Burner</xsl:text>
</xsl:when>
<xsl:when test="$time >= 30 and $time <= 60">
<xsl:text>Medium Burner</xsl:text>
</xsl:when>
<xsl:when test="$time < 30">
<xsl:text>Quick and Easy</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Undefined</xsl:text>
</xsl:otherwise>
</xsl:choose>
</recipe>
</td>
<td>
<xsl:value-of select="description" />
</td>
<td>
<xsl:value-of select="servings" />
</td>
<td>
<xsl:value-of select="preparetion_Time" />
</td>
<td>
<xsl:value-of select="cooking_Time" />
</td>
<td>
<xsl:value-of select="passiveTime" />
</td>
<td>
<xsl:value-of select="difficulty" />
</td>
<td>
<xsl:for-each select="ingredients">
<xsl:for-each select="ingredient">
<li>
<xsl:value-of select="." />
</li>
</xsl:for-each>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
它通过使用 normalize-space(...)
改进了排序,并使用 xsl:choose
而不是内联 if
。
关于javascript - 为什么我在 XSLT 转换期间出现此错误 : XSLT transformation failed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60460609/