XSL使用模板获取所选节点的子节点

时间:2014-02-18 07:16:28

标签: xml xslt xpath

我有一个XML,其中GRTReport下有多个GRTSource节点。我只想要GRTSource的前5个节点,我想访问表的详细信息,而不管表是否为子项。

这是演示文件:

<GRTReport>
<GRTSource>
        <title>Unified CM Cluster Name</title>
        <comment>Lists the cluster name from the Enterprise Parameter and the publisher server name/IP.</comment>
        <title></title>
        <comment></comment>
        <table summary='Cluster Information'>
            <row>
                <cell style='header'>Cluster Name</cell>
                <cell style='header'>Publisher Name/IP</cell>
            </row>
            <row>
                <cell>xyzw</cell>
                <cell>xyz1234</cell>
            </row>
        </table>
    </GRTSource>
    <GRTSource>
        <div>
        <title>Unified CM Cluster Name</title>
        <comment>Lists the cluster name from the Enterprise Parameter and the publisher server name/IP.</comment>
        <title></title>
        <comment></comment>
        <table summary='Cluster Information'>
            <row>
                <cell style='header'>Cluster Name</cell>
                <cell style='header'>Publisher Name/IP</cell>
            </row>
            <row>
                <cell>xyzw</cell>
                <cell>xyz1234</cell>
            </row>
        </table>
    </div>
    </GRTSource>
    </GRTReport>

这是我的xsl:

<xsl:template match="/">
        <html>
            <body>  
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="GRTReport/GRTSource[position() &lt; 6]">

        <fieldset class="reportSourceFieldset">
            <legend>
                <xsl:value-of select="title[1]"/>
            </legend>
            <xsl:apply-templates select="descendant::table"/>
        </fieldset>
        <br />

    </xsl:template>

    <xsl:template match="table">
        <table class="reportData">
            <tr class="cuesTableBg">    
                <xsl:for-each select="row[position() = 1]/cell">
                    <th>
                        <pre>
                            <xsl:value-of select="current()"/>
                        </pre>
                    </th>
                </xsl:for-each>
            </tr>

            <xsl:for-each select="row[position() &gt; 1]">
                <tr>
                    <xsl:for-each select="cell">
                        <xsl:choose>
                        <xsl:when test="current()[contains(@style,'fixedFormat')]">
                        <td>
                            <pre><xsl:value-of select="current()"/></pre>
                        </td>
                        </xsl:when>
                        <xsl:otherwise>
                        <td>
                        <span>
                            <xsl:value-of select="current()"/></span>
                        </td>
                        </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </table>
        <br />
    </xsl:template>

不知何故,它将GRTSources的计数提高到5,但查询也接受了GRSource计数为5之外的表。它实际上考虑了页面上的所有表格,而不仅仅是前5个GRTSource

2 个答案:

答案 0 :(得分:2)

执行此操作时:

<xsl:template match="/">
    <html>
        <body>  
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

您正在不加选择地将模板应用于当前节点的所有子节点(在本例中为根节点)。 built-in template rules“允许在没有通过样式表中的显式模板规则成功匹配模式的情况下继续递归处理”。这意味着模板从此处应用到<GRTReport>,并从那里应用到先前未被更明确的规则匹配的<GRTSource>元素,并从那里应用到他们的孩子 - 并且突然间您有一个expicit匹配的模板规则。

尝试将上述内容更改为:

<xsl:template match="/">
        <html>
            <body>  
                <xsl:apply-templates select="GRTReport/GRTSource[position() &lt; 6]"/>
            </body>
        </html>
</xsl:template>

然后你可以改变这个:

<xsl:template match="GRTReport/GRTSource[position() &lt; 6]">

为:

<xsl:template match="GRTReport/GRTSource">

答案 1 :(得分:0)

也许你想考虑一下:

<xsl:template match="GRTReport/GRTSource">
        <xsl:choose>
            <xsl:when test="count(preceding-sibling::GRTSource) + 1 &lt; 6">
                <fieldset class="reportSourceFieldset">
                    <legend>
                        <xsl:value-of select="descendant::title[1]"/>
                    </legend>
                    <xsl:apply-templates select="descendant::table"/>
                </fieldset>
                <br />
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
</xsl:template>
相关问题