XSLT 将项目与 Class 匹配

XSLT Match Items With Class

我正在做一个相当简单的 XSLT 模板,但似乎有问题。

我试图遍历 HTML table 中的每个元素,如果它有一个特定的 class,那么它应该打印一些东西,如果没有,它应该打印其他东西。

节选HTML。我想将 an 与 class“instructionRow”相匹配。


                  <tr role="row" class="odd">
                    <td>1</td><td class="itemCell">Burger</td>
                    <td>15.00</td>
                    <td>Mains</td>
                    </tr>
                    <tr role="row" class="even instructionRow">
                    <td class="invisibleText">1</td>
                    <td class="itemCell">No Pineapple</td>
                    <td>1.50</td><td class="invisibleText"></td>
                  </tr>

这是我正在尝试的 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<order timestamp="{current-dateTime()}">
<xsl:for-each select="table/tbody/tr">
<xsl:choose>
    <xsl:when test="//tr[contains(@class, 'instructionRow')]">
        Instruction
    </xsl:when>
    <xsl:otherwise>
        Not Instruction
    </xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</order>
</xsl:template>
</xsl:stylesheet>

然而,测试似乎总是 return 肯定并打印“Instruction”,即使我将要匹配的字符串设置为...任何内容。在上面的例子中,转换后的结果应该是

Not Instruction

Instruction

A fiddle: http://xsltransform.net/6qtmFHd

谢谢

<xsl:for-each select="table/tbody/tr">

当前节点是tr。所以,改变

<xsl:when test="//tr[contains(@class, 'instructionRow')]">

(不检查当前 tr,而是检查文档中的每个 tr)到

<xsl:when test="contains(@class, 'instructionRow')">

为了测试循环迭代的每个 tr/@class