如何使用元素对输出的表中的元素进行排序?我想使用元素中的元素。我尝试将元素放在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>这是我的.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>发布于 2014-10-14 20:26:50
我想输出数据,以便按照元素按字母顺序排序。因此,所有的“民主党”总统都会首先出现。
那么,让我们用一个例子来检验一下,元素的自然顺序并不意味着民主党总统在产出中是第一位的:
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>然后,整理你的数据。目前,您正在匹配xsl:sort的模板上下文中应用president,如下所示:
<xsl:apply-templates select="party">
<xsl:sort select="party"/>
</xsl:apply-templates>但是您希望排序的单元不是party,而是president元素。只是排序条件是party元素的文本内容,它是president的子元素。
因此,为了达到预期效果,您必须在XML层次结构中将排序应用到更高的位置。您必须在presidents的上下文中进行排序,其中模板应用于president元素。
样式表
<?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输出
正如你所看到的,总统和民主党现在是第一位的。
<!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>https://stackoverflow.com/questions/26368839
复制相似问题