创建后在 xsl 中对表进行排序 [英] Sorting a table in xsl after creation

查看:25
本文介绍了创建后在 xsl 中对表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的 XML 中的两个节点集创建一个 HTML 表,然后按 @AD 对其进行排序.

I am trying to create a HTML table from two node sets in my XML and then sort it by @AD.

我可以使用 <xsl:sort select="@AD" order="ascending"/> 在单独的 for-each 循环中进行排序,但我想对整个表格进行排序.

I can sort within individual for-each loops using <xsl:sort select="@AD" order="ascending" />, but I want to sort the whole table.

<xsl:template match="*/Sync/AP">
    <table border="1">
        <tr>
            <th>AD</th>
            <th>GCD</th>
            <th>ClearAttribute</th>
        </tr>
        <xsl:for-each select="./*">
        <tr>
            <td><xsl:value-of select="@AD"/></td>
            <td><xsl:value-of select="@GCD"/></td>
            <td><xsl:value-of select="@ClearAttribute"/></td>
        </tr>       
        </xsl:for-each>
        <!-- Also Append the Common attributes to each region -->
        <xsl:for-each select="../Common/*">
        <tr>
            <td><xsl:value-of select="@AD"/></td>
            <td><xsl:value-of select="@GCD"/></td>
            <td><xsl:value-of select="@ClearAttribute"/></td>
        </tr>       
        </xsl:for-each>
    </table>
</xsl:template>

推荐答案

不要制作两个单独的 .选择要显示的所有节点,并一步对它们进行排序.

Don't make two separate <xsl:for-each>. Select all the nodes you want to display and sort them in one step.

联合运算符 | 用于此:

The union operator | is used for this:

<xsl:template match="Sync/AP">
    <table border="1">
        <tr>
            <th>AD</th>
            <th>GCD</th>
            <th>ClearAttribute</th>
        </tr>
        <xsl:for-each select="./* | ../Common/*">
            <xsl:sort select="@AD" order="ascending" />
            <tr>
                <td><xsl:value-of select="@AD"/></td>
                <td><xsl:value-of select="@GCD"/></td>
                <td><xsl:value-of select="@ClearAttribute"/></td>
            </tr>       
        </xsl:for-each>
    </table>
</xsl:template>

<小时>

注意:尽管 match 表达式看起来像 XPath,但它们并不是真正的 XPath.这是不必要的:


Note: Even though match expressions look like XPath, they are not really XPath. This is unnecessary:

<xsl:template match="*/Sync/AP">

你可以改用这个:

<xsl:template match="Sync/AP">

甚至这个:

<xsl:template match="AP">

除非您明确想确保只有 父级匹配.

unless you explicitly want to make sure that only <AP> with a <Sync> parent are matched.

这篇关于创建后在 xsl 中对表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆