使用<xsl:sort not =“”functional =“”against =“”assigned =“”element =“”

时间:2016-10-19 03:39:59

标签: xml sorting xslt

=“”

我是对XSLT的使用很新(不使用xsl:for-each可能是罪魁祸首)并且遇到了一个问题,试图让派对元素按照预期运行进行排序。

我有一个包含以下内容的XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="president_table_PROBz.xsl"?>

        <presidents xmlnsxsi="http//www.w3.org/2001/XMLSchema-instance"
            xsinoNamespaceSchemaLocation="http//www.du.edu/~mschwart/xml/president.xsd"
            date="2012-08-18">
            <president>
                <number>1</number>
                <name>George Washington</name>
                <birthday>2/22/1732</birthday>
                <took_office>1789-04-30</took_office>
                <left_office>1797-03-04</left_office>
                <party>no party</party>
                <term>
                    <number>1</number>
                    <vice_president>John Adams</vice_president>
                </term>
                <term>
                    <number>2</number>
                    <vice_president>John Adams</vice_president>
                </term>
            </president>

            <president>
                <number>2</number>
                <name>John Adams</name>
                <birthday>10/30/1735</birthday>
                <took_office>1797-03-04</took_office>
                <left_office>1801-03-04</left_office>
                <party>Federalist</party>
                <term>
                    <number>3</number>
                    <vice_president>Thomas Jefferson</vice_president>
                </term>
            </president>
            <president>
                <number>3</number>
                <name>Thomas Jefferson</name>
                <birthday>4/13/1743</birthday>
                <took_office>1801-03-04</took_office>
                <left_office>1809-03-04</left_office>
                <party>Democratic-Republican</party>
                <term>
                    <number>4</number>
                    <vice_president>Aaron Burr</vice_president>
                </term>
                <term>
                    <number>5</number>
                    <vice_president>George Clinton</vice_president>
                </term>
            </president>
            <president>
                <number>4</number>
                <name>James Madison</name>
                <birthday>3/16/1751</birthday>
                <took_office>1809-03-04</took_office>
                <left_office>1817-03-04</left_office>
                <party>Democratic-Republican</party>
                <term>
                    <number>6</number>
                    <vice_president>George Clinton</vice_president>
                </term>
                <term>
                    <number>7</number>
                    <vice_president>Elbridge Gerry</vice_president>
                </term>
            </president></presidents>

一个XSLT文档,其中我为其他请求添加了项目,但现在需要按方对数据进行排序:

<xsl:template match="/">
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="president_table.css" />
            <title>Table of US Presidents</title>
        </head>
        <body>
            <h1>Table of US Presidents</h1>
            <table>
            <tr>
                <th>Name</th>
                <th>Birthday</th>
                <th>Took Office</th>
                <th>Left Ofice</th>
                <th>Party</th>
                <th>Vice President(s)</th>
            </tr>
            <xsl:apply-templates select="presidents">
                <xsl:sort select="party" order="descending"/>
                </xsl:apply-templates>
            </table>
        </body>
    </html>
</xsl:template>
<xsl:template match="president">
    <tr>
        <td><xsl:apply-templates select="name"/></td>
        <td><xsl:apply-templates select="birthday"/></td>
        <td><xsl:apply-templates select="took_office"/></td>
        <td><xsl:apply-templates select="left_office"/></td>
        <td>
            <xsl:apply-templates select="party">
            </xsl:apply-templates>
            <xsl:value-of select="party"/>
        </td>
        <td>
            <xsl:apply-templates select="term"/>
        </td>
    </tr>
</xsl:template>
<xsl:template match="term">
    <xsl:number value="position()" format="1. " />
    <xsl:value-of select="vice_president" /><br />
</xsl:template>
<xsl:template match="party">
    <xsl:choose>
        <xsl:when test=". = 'Federalist'">
            <xsl:attribute name="class">Federalist</xsl:attribute>
        </xsl:when>
        <xsl:when test=". = 'Democratic-Republican'">
            <xsl:attribute name="class">Democratic-Republican</xsl:attribute>
        </xsl:when>
        <xsl:when test=". = 'Democratic'">
            <xsl:attribute name="class">Democratic</xsl:attribute>
        </xsl:when>
        <xsl:when test=". = 'Republican'">
            <xsl:attribute name="class">Republican</xsl:attribute>
        </xsl:when>
        <xsl:when test=". = 'Whig'">
            <xsl:attribute name="class">Whig</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:attribute name="class">noparty</xsl:attribute>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

我已经四处搜索,并没有具体说明发生了什么,我已经按照一些例子的说明来尝试排序。

感谢您提前获得任何帮助!!

1 个答案:

答案 0 :(得分:0)

partypresident的孩子,不是presidents。尝试将您的应用模板更改为select="presidents/president"