javascript - 为什么我在 XSLT 转换期间出现此错误 : XSLT transformation failed?

标签 javascript html css xml xslt

我正在尝试根据 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 &gt; 60">
                        <xsl:text>Slow Burner</xsl:text>
                    </xsl:when>
                    <xsl:when test="$time &gt;= 30 and $time &lt;= 60">
                        <xsl:text>Medium Burner</xsl:text>
                    </xsl:when>
                    <xsl:when test="$time &lt; 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/

相关文章:

html - 在图像下定位文本 - HTML 电子邮件

html - 过渡不是双向的

javascript - 调整浏览器大小时重置 css 或脚本

javascript - 使用 getElementById 的多个元素

javascript - 使用自己的服务器(通用提供商)设置 Electron 自动更新程序

javascript - oninvalid 事件和 Internet Explorer?

php - 客户端处理与服务器端处理,哪个快?

javascript - R Shiny 仪表板: specifying div style width argument as percentage to fit a resizeable JS plot

css - 列中的 Kendo UI 网格输入样式

html - 可扩展的侧滚动问题