Xml 使用<;xsl:apply templates>;及<;xsl:sort>;

Xml 使用<;xsl:apply templates>;及<;xsl:sort>;,xml,xslt,Xml,Xslt,如何使用元素对正在输出的表中的元素进行排序?我想使用元素中的元素。我尝试将元素放在xsl文档中,但它没有对元素进行排序 以下是我的xml: <presidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="president.xsd" date="2014-09-24" > <president> <number&g

如何使用元素对正在输出的表中的元素进行排序?我想使用元素中的元素。我尝试将元素放在xsl文档中,但它没有对元素进行排序

以下是我的xml:

<presidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="president.xsd" date="2014-09-24" >
    <president>
        <number>1</number>
        <name>George Washington</name>
         <birthday>1732-02-22</birthday>
        <took_office>1789-04-30</took_office>
        <left_office>1797-03-04</left_office>
        <party>no party</party>
        <term>
            <term_number>1</term_number>
            <vice_president>John Adams</vice_president> 
        </term>
        <term>
            <term_number>2</term_number>
            <vice_president>John Adams</vice_president>
        </term>
    </president>

    <president>
        <number>2</number>
        <name>John Adams</name>
         <birthday>1735-10-30</birthday>
        <took_office>1797-03-04</took_office>
        <left_office>1801-03-04</left_office>
        <party>Federalist</party>
        <term>
            <term_number>3</term_number>
            <vice_president>Thomas Jefferson</vice_president>
        </term>
    </president>
</presidents>

1.
乔治华盛顿
1732-02-22
1789-04-30
1797-03-04
没有聚会
1.
约翰·亚当斯
2.
约翰·亚当斯
2.
约翰·亚当斯
1735-10-30
1797-03-04
1801-03-04
联邦主义者
3.
托马斯·杰斐逊
这是我的.xsl:

<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 Office</th>
                <th>Party</th>
                <th>Vice President</th>
            </tr>
            <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:sort select="party"/>
    </xsl:apply-templates>
    </td>
    <td>
    <xsl:for-each select="term">
        <xsl:number value="position()" format="1. " />
        <xsl:value-of select="vice_president" /><br />
    </xsl:for-each>
    </td>
</tr>
</xsl:template>

美国总统一览表
美国总统一览表
名称
生日
上台
离职
聚会
副总裁

我想输出数据,以便按照元素的字母顺序进行排序。所以所有的“民主党”总统都会首先出现

然后,让我们用一个例子来测试,在这个例子中,要素的自然顺序并不意味着民主总统在产出中排在第一位:

XML输入

<presidents date="2014-09-24" >
    <president>
        <number>1</number>
        <name>George Washington</name>
         <birthday>1732-02-22</birthday>
        <took_office>1789-04-30</took_office>
        <left_office>1797-03-04</left_office>
        <party>Federalist</party>
        <term>
            <term_number>1</term_number>
            <vice_president>John Adams</vice_president> 
        </term>
        <term>
            <term_number>2</term_number>
            <vice_president>John Adams</vice_president>
        </term>
    </president>

    <president>
        <number>2</number>
        <name>John Adams</name>
         <birthday>1735-10-30</birthday>
        <took_office>1797-03-04</took_office>
        <left_office>1801-03-04</left_office>
        <party>Democratic</party>
        <term>
            <term_number>3</term_number>
            <vice_president>Thomas Jefferson</vice_president>
        </term>
    </president>
</presidents>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" doctype-public="XSLT-compat" indent="yes" /> 

    <xsl:template match="/presidents">
    <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 Office</th>
                <th>Party</th>
                <th>Vice President</th>
            </tr>
            <xsl:apply-templates select="president">
                <xsl:sort select="party"/>
            </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"/>
    </td>
    <td>
    <xsl:for-each select="term">
        <xsl:number value="position()" format="1. " />
        <xsl:value-of select="vice_president" /><br />
    </xsl:for-each>
    </td>
</tr>
</xsl:template>

</xsl:transform>
但是您要排序的单元不是
party
,而是
president
元素。只是排序条件是
party
元素的文本内容,它是
president
的子元素

因此,为了达到预期的效果,必须在XML层次结构的更高层应用排序。您必须在
总统
的上下文中进行排序,其中模板应用于
总统
元素

样式表

<presidents date="2014-09-24" >
    <president>
        <number>1</number>
        <name>George Washington</name>
         <birthday>1732-02-22</birthday>
        <took_office>1789-04-30</took_office>
        <left_office>1797-03-04</left_office>
        <party>Federalist</party>
        <term>
            <term_number>1</term_number>
            <vice_president>John Adams</vice_president> 
        </term>
        <term>
            <term_number>2</term_number>
            <vice_president>John Adams</vice_president>
        </term>
    </president>

    <president>
        <number>2</number>
        <name>John Adams</name>
         <birthday>1735-10-30</birthday>
        <took_office>1797-03-04</took_office>
        <left_office>1801-03-04</left_office>
        <party>Democratic</party>
        <term>
            <term_number>3</term_number>
            <vice_president>Thomas Jefferson</vice_president>
        </term>
    </president>
</presidents>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" doctype-public="XSLT-compat" indent="yes" /> 

    <xsl:template match="/presidents">
    <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 Office</th>
                <th>Party</th>
                <th>Vice President</th>
            </tr>
            <xsl:apply-templates select="president">
                <xsl:sort select="party"/>
            </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"/>
    </td>
    <td>
    <xsl:for-each select="term">
        <xsl:number value="position()" format="1. " />
        <xsl:value-of select="vice_president" /><br />
    </xsl:for-each>
    </td>
</tr>
</xsl:template>

</xsl:transform>

美国总统一览表
美国总统一览表
名称
生日
上台
离职
聚会
副总裁

HTML输出

<presidents date="2014-09-24" >
    <president>
        <number>1</number>
        <name>George Washington</name>
         <birthday>1732-02-22</birthday>
        <took_office>1789-04-30</took_office>
        <left_office>1797-03-04</left_office>
        <party>Federalist</party>
        <term>
            <term_number>1</term_number>
            <vice_president>John Adams</vice_president> 
        </term>
        <term>
            <term_number>2</term_number>
            <vice_president>John Adams</vice_president>
        </term>
    </president>

    <president>
        <number>2</number>
        <name>John Adams</name>
         <birthday>1735-10-30</birthday>
        <took_office>1797-03-04</took_office>
        <left_office>1801-03-04</left_office>
        <party>Democratic</party>
        <term>
            <term_number>3</term_number>
            <vice_president>Thomas Jefferson</vice_president>
        </term>
    </president>
</presidents>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" doctype-public="XSLT-compat" indent="yes" /> 

    <xsl:template match="/presidents">
    <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 Office</th>
                <th>Party</th>
                <th>Vice President</th>
            </tr>
            <xsl:apply-templates select="president">
                <xsl:sort select="party"/>
            </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"/>
    </td>
    <td>
    <xsl:for-each select="term">
        <xsl:number value="position()" format="1. " />
        <xsl:value-of select="vice_president" /><br />
    </xsl:for-each>
    </td>
</tr>
</xsl:template>

</xsl:transform>
如你所见,民主党的总统现在是第一位

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <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 Office</th>
            <th>Party</th>
            <th>Vice President</th>
         </tr>
         <tr>
            <td>John Adams</td>
            <td>1735-10-30</td>
            <td>1797-03-04</td>
            <td>1801-03-04</td>
            <td>Democratic</td>
            <td>1. Thomas Jefferson<br></td>
         </tr>
         <tr>
            <td>George Washington</td>
            <td>1732-02-22</td>
            <td>1789-04-30</td>
            <td>1797-03-04</td>
            <td>Federalist</td>
            <td>1. John Adams<br>2. John Adams<br></td>
         </tr>
      </table>
   </body>
</html>

美国总统一览表
美国总统一览表
名称
生日
上台
离职
聚会
副总裁
约翰·亚当斯
1735-10-30
1797-03-04
1801-03-04
民主的
1.托马斯·杰斐逊
乔治华盛顿 1732-02-22 1789-04-30 1797-03-04 联邦主义者 1.约翰·亚当斯
2。约翰·亚当斯

总统
要素中,一个
政党
要素不得超过一个。为什么它需要分类?你想分类别的吗?好的,对不起,我想我没有很好地描述我的问题。我想输出数据,以便按照元素的字母顺序进行排序。所以所有的“民主党”总统都会首先出现。谢谢!我一开始试过了,结果只显示了表头,其余的数据消失了。我正在firefox中预览xml文件。好的,我想出来了,我不知道为什么它能与这个解决方案一起工作。我改为:@esgg不,你一开始并没有这么做。我敢打赌,您的第一个模板与
/
匹配,而我的第一个模板与
/presidents
匹配。我尝试了太多的变体,我都快疯了!我很高兴你帮我弄明白了!谢谢:)