测试选择时不接受XSLT

时间:2011-07-17 17:50:54

标签: xml xslt testing

我一直在为老板做点什么,这是一个展示票务系统网络服务的网站。 Web服务基于查询吐出XML文件。我的问题是,当我尝试为输出设置条件时,我得到一个错误,或者当包含每个票证的XML条目的数组明显与我在测试中的匹配时,它会一直跳到“otherwise”条目。我是XSLT的新手,只有我已经研究过的东西。有人可以看看我有什么,并解释为什么这不起作用?我知道这只是与测试表达式有关。

XML(数组输出,没有原始XML文件)

    Array
(
    [RowCount] => 3
    [HasError] => false
    [ErrorMessage] => Array
        (
        )

    [StackTrace] => Array
        (
        )

    [IncidentList] => Array
        (
            [Incident] => Array
                (
                    [0] => Array
                        (
                            [GroupName] => EXTERNAL_SUPPORT
                            [IncidentNumber] => 229178
                            [OpenDateAndTime] => 2011-05-09T10:42:33
                            [State] => O
                            [StatusID] => EMAIL WIP
                            [SubjectID] => MAGIC
                            [UrgencyID] => NORMAL
                        )

                    [1] => Array
                        (
                            [GroupName] => CISS SYSTEMS
                            [IncidentNumber] => 203863
                            [OpenDateAndTime] => 2010-05-25T09:16:55
                            [State] => C
                            [StatusID] => CLOSED
                            [SubjectID] => ULID EXPIRATION
                            [UrgencyID] => NORMAL
                        )

                    [2] => Array
                        (
                            [GroupName] => HELP DESK 1ST LEVEL
                            [IncidentNumber] => 186909
                            [OpenDateAndTime] => 2009-09-11T09:58:44
                            [State] => C
                            [StatusID] => CLOSED
                            [SubjectID] => QUESTION
                            [UrgencyID] => NORMAL
                        )

                )

        )

)

我正在使用的是这个XSLT片段,它根据状态为O,C或其他方式显示一个表,显示一条没有要显示的票据的消息。

<xsl:template match="/">
    <table id="myTable" class="list">
        <tr>
            <td class="title">Incident</td>
            <td class="title">Category</td>
            <td class="title">State</td>
            <td class="title">Status</td>
            <td class="title">Date</td>
        </tr>
        <xsl:choose>
            <xsl:when test="State = 'O'">
                <xsl:for-each select="Results/IncidentList/Incident">
                    <tr>
                        <td>#<xsl:value-of select="IncidentNumber"/></td>
                        <td><xsl:value-of select="SubjectID"/></td>
                        <td>OPEN</td>
                        <td><xsl:value-of select="StatusID"/></td>
                        <td>
                            <xsl:call-template name="formatDate">
                                <xsl:with-param name="dateTime" select="OpenDateAndTime" />
                            </xsl:call-template>
                        </td>
                    </tr>
                </xsl:for-each>
            </xsl:when>
            <xsl:when test="State = 'C'">
                <xsl:for-each select="Results/IncidentList/Incident">
                    <tr>
                        <td class="closed">#<xsl:value-of select="IncidentNumber"/></td>
                        <td class="closed"><xsl:value-of select="SubjectID"/></td>
                        <td class="closed">CLOSED</td>
                        <td class="closed"></td>
                        <td class="closed">
                            <xsl:call-template name="formatDate">
                                <xsl:with-param name="dateTime" select="OpenDateAndTime" />
                            </xsl:call-template>
                        </td>
                    </tr>
                </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
                <tr>
                    <td colspan="5">There are no incidents to display</td>
                </tr>
            </xsl:otherwise>
        </xsl:choose>
    </table>
</xsl:template>

</xsl:stylesheet>

我已经尝试过测试表达式的每次迭代,但我得到错误或直接跳到其他地方。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

你可能意味着这而不是'xsl:when's:

<xsl:for-each select="Results/IncidentList/Incident[State = 'O']"> 

<xsl:for-each select="Results/IncidentList/Incident[State = 'C']">

等。示例中“when”条件的上下文不是Incident,而是XML文档的根。所以你也可以选择:

<xsl:for-each select="Results/IncidentList/Incident[State]">
   <xsl:choose>
      <xsl:when test="State = 'O'">..</xsl:when>
      <xsl:when test="State = 'C'">..</xsl:when>
      <Xsl:otherwise>..</xsl:otherwise>
   </xsl:choose>
</xsl:for-each>

另一种选择是使用模板:

<xsl:template match="/">
   ..
   <xsl:apply-templates select="Results/IncidentList/Incident[State]" />
   ..
</xsl:template>

<xsl:template match="Results/IncidentList/Incident[State = 'O']">..</xsl:template>
<xsl:template match="Results/IncidentList/Incident[State = 'C']">..</xsl:template>
..

答案 1 :(得分:0)

  • 您对State的值的测试需要位于上下文为xsl:for-each的{​​{1}}内,并且Incident的相对路径将匹配。

  • 更“干”的方法是将State移到xsl:choose内部,为<td>应用不同的值,并避免重复相同的列逻辑所有其他人。

  • 当您没有State个元素时,是否要生成包含“没有要显示的事件”的行?在您当前的实现中(和在已发布的其他答案中),如果有Results/IncidentsList/IncidentResults/IncidentList/Incident值既不是“O”或“C”,您只会收到该消息。如果没有,你就不会产生那一行。

以下是解决上述问题的实现:

State