XSLT如何检查数组中是否存在元素

时间:2013-08-22 09:14:11

标签: xslt

XML:

<Calendars xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Urnik.xsd">
    <Calendar>
        <Name>Marko</Name>
        <Days>
            <Day>
                <Date>2013-05-13</Date>
                <DayType>1</DayType>
                <DayWorking>1</DayWorking>
                <WorkingTimes>
                    <WorkingTime>
                        <FromTime>08:00</FromTime>
                        <ToTime>11:00</ToTime>
                        <Name>Izpit Matematika</Name>
                        <Owner>Marko</Owner>
                        <Category>
                            <School Professor="111" Room="1" Subject="882" />
                        </Category>
                    </WorkingTime>
                    <WorkingTime>
                        <FromTime>13:00</FromTime>
                        <ToTime>17:00</ToTime>
                        <Name>Vaje APZ</Name>
                        <Owner>Marko</Owner>
                        <Category>
                            <School Professor="222" Room="11" Subject="881"/>
                        </Category>
                    </WorkingTime>      
                    <WorkingTime>
                        <FromTime>20:00</FromTime>
                        <ToTime>22:00</ToTime>
                        <Name>Vaje aaaaaa</Name>
                        <Owner>Marko</Owner>
                        <Category>
                            <School Professor="222" Room="11" Subject="881"/>
                        </Category>
                    </WorkingTime>                      
                </WorkingTimes>
            </Day>
            <Day>
                <Date>2013-05-14</Date>
                <DayType>2</DayType>
                ...
            </Day>

XSLT:

<xsl:for-each select="Calendar/Days/Day">   
    <xsl:choose>
        <xsl:when test="DayType = 1">
            <xsl:variable name="vTransfers" select="child::*/WorkingTime"/>                 

如果vTransfers中存在08:00,我可以以某种方式检查此变量FromTime吗?我不想循环它因为我需要将它放在正确的TD行中。我现在处于此TD状态,我想检查此数组中08:00中是否存在FromTime

我尝试像

这样的东西
<xsl:when test="contains($vTransfers/ToTime, '08:00')">
                        <xsl:text>IN</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>                                     
                        <xsl:text>OUT</xsl:text>
                    </xsl:otherwise>

但总是只拿一个而不检查整个数组。

1 个答案:

答案 0 :(得分:3)

如果将节点集传递给contains,则会将其转换为字符串。这意味着仅采用第一个节点的字符串值。尝试类似的东西:

<xsl:when test="$vTransfers[contains(ToTime, '08:00')]">

这也应该有效:

<xsl:when test="WorkingTimes/WorkingTime/ToTime = '08:00'">