如何在XSL中比较来自不同父项的两个元素

时间:2011-07-28 21:59:49

标签: html xslt-1.0 xpath-1.0

我的HTML日程安排如下:

+------+--------+----------+---------+----------+--------+
| Time | Monday | Tuesday  |Wednesday| Thursday | Friday | 
+======+========+==========+=========+==========+========+
| 8:00 | event1 |          |         |          |        |
+------+--------+----------+---------+----------+--------+
| 9:00 | event1 |          |         |          |        |
+------+--------+----------+---------+----------+--------+
| 10:00| event1 |          | event2  |          |        |
|      |        |          | event3  |          |        |
+------+--------+----------+---------+----------+--------+
| 11:00|        |          | event3  |          |        |
+------+--------+----------+---------+----------+--------+
| 12:00|        |          | event3  |          |        |
+------+--------+----------+---------+----------+--------+
<table>
    <tr>
        <td> Time      </td>
        <td> Monday    </td>
        <td> Tuesday   </td>
        <td> Wednesday </td>
        <td> Thursday  </td>
        <td> Friday    </td>
    </tr>
    <tr>
        <td> 8:00      </td>
        <td> event1    </td>
        <td>           </td>
        <td>           </td>
        <td>           </td>
        <td>           </td>
    </tr>
    <tr>
        <td> 9:00      </td>
    <td> event1    </td>
        <td>           </td>
        <td>           </td>
        <td>           </td>
        <td>           </td>
    </tr>
    <tr>
        <td> 10:00     </td>
        <td> event1    </td>
        <td>           </td>
        <td> event2 </br> event3    </td>     <!-- Comparing to this one while reading the one below -->
        <td>           </td>
        <td>           </td>
    </tr>
    <tr>
        <td> 11:00     </td>
        <td>           </td>
        <td>           </td>
        <td> event3    </td>                  <!-- Reading this one -->
        <td>           </td>
        <td>           </td>
    </tr>
    <tr>
        <td> 12:00     </td>
        <td>           </td>
        <td>           </td>
        <td> event3    </td>
        <td>           </td>
        <td>           </td>
    </tr>
</table>

是否有可能在单个for-each循环中访问与其父级相比处于相同位置的<td>,但是在之前的<tr>中?

我尝试过这样的事情,除了同时发生的event2和event3之外,它的工作正常。

<xsl:variable name="periodes">
    <xsl:for-each select="table/tr/td[position() = 2]"> <!-- And to the same for every columns -->
    <xsl:if test="not(.=preceding::td)">
        <starts><xsl:value-of select="../td[position() =1]"/></starts>
    </xsl:if>
    <xsl:if test="not(.=following::td)">
        <ends><xsl:value-of select="../td[position() =1]"/></ends>
            <eventName><xsl:value-of select="."/></eventName>
        </xsl:if>
    </xsl:for-each>
</xsl:variable>
<xsl:for-each select="exslt:node-set($periodes)/*">
    <day>1</day>
    <eventName><xsl:value-of select="eventName"/></eventName>
    <starts><xsl:value-of select="starts"/></starts>
    <ends><xsl:value-of select="ends"/></ends>
</xsl:for-each>

除此之外不能正常工作,这种方法有点烦人,因为我需要5个不同的for-each循环。他们是一种可以实现这一目标的方式吗?

输出应该类似

<events>
    <event>
        <day>1</day>
        <eventName>event1</eventName>
        <starts>8:00</starts>
        <ends>11:00</ends>
    </event>
    <event>
        <day>3</day>
        <eventName>event2</eventName>
        <starts>10:00</starts>
        <ends>11:00</ends>
    </event>
    <event>
        <day>3</day>
        <eventName>event3</eventName>
        <starts>10:00</starts>
        <ends>13:00</ends>
    </event>
</events>

0 个答案:

没有答案