如何使用元素对我输出的表格中的元素进行排序?我想使用元素中的元素。我尝试将元素放在我的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>
答案 0 :(得分:1)
我想输出数据,以便按字母顺序按元素排序。因此,所有“民主党”总统都将首先出现。
然后,让我们测试一个例子,其中元素的自然顺序不表明民主党总统在输出中排在第一位:
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>