使用xsl排序:sort

时间:2016-01-15 14:18:16

标签: sorting xslt

我有XML文件(一些服务文档)如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<apis>
    <api min="" max="">
        <resource name="C">
            <description>C from beggining to the end</description>
        </resource>
    </api>
    <api min="2.2" max="">
        <resource name="B">
            <description>B from 2.2 to the end</description>
        </resource>
        <resource name="A">
            <description>A from 2.2 to the end</description>
        </resource>
    </api>
</apis>

和XSL文件将XML转换为html:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:param name="api-version" select="2.2"></xsl:param>

    <xsl:template match="apis">
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            </head>
            <body>
                <table border="1">
                    <th colspan="2">
                        <xsl:text>Doc for API: </xsl:text>
                        <xsl:value-of select="$api-version"/>
                    </th>
                    <xsl:call-template name="handleApis"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="handleApis">
        <xsl:for-each select="api[(@min='' or @min&lt;=$api-version) and (@max='' or @max&gt;$api-version)]">
            <xsl:for-each select="resource">
                <xsl:call-template name="handleResource"/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="handleResource">
        <tr>
            <td>
                <xsl:value-of select="@name"/>
            </td>
            <td>
                <xsl:value-of select="description"/>
            </td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

现在我想按资源属性名称对结果进行排序。

我试图把

<xsl:sort select="@name" order="ascending"/>

内部for-each用于资源节点,但是你知道它不会在不同的api节点之间对资源进行排序,结果是

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   </head>
   <body>
      <table border="1">
         <th colspan="2">Doc for API: 2.2</th>
         <tr>
            <td>C</td>
            <td>C from beggining to the end</td>
         </tr>
         <tr>
            <td>A</td>
            <td>A from 2.2 to the end</td>
         </tr>
         <tr>
            <td>B</td>
            <td>B from 2.2 to the end</td>
         </tr>
      </table>
   </body>
</html>

添加

<xsl:sort select="resource/@name" order="ascending"/>

for-each for api节点内部也无法正常工作,因为sort只需要select atrribute中的一个项目。

有一些方法可以对它进行排序以获得输出中的顺序A,B,C吗?

1 个答案:

答案 0 :(得分:0)

简单地说:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="api-version" select="2.2"/>

<xsl:template match="/apis">
    <html>
        <head/>
        <body>
            <table border="1">
                <th colspan="2">
                    <xsl:text>Doc for API: </xsl:text>
                    <xsl:value-of select="$api-version"/>
                </th>
                <xsl:apply-templates select="api[(@min='' or @min &lt;= $api-version) and (@max='' or @max &gt; $api-version)]/resource">
                    <xsl:sort select="@name"/>
                </xsl:apply-templates>  
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="resource">
    <tr>
        <td>
            <xsl:value-of select="@name"/>
        </td>
        <td>
            <xsl:value-of select="description"/>
        </td>
    </tr>
</xsl:template>

</xsl:stylesheet>
相关问题