gpt4 book ai didi

xml - 使用 and

转载 作者:数据小太阳 更新时间:2023-10-29 02:35:06 24 4
gpt4 key购买 nike

如何使用元素对正在输出的表格中的元素进行排序?我想使用元素内的元素。我尝试将该元素放在我的 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>

最佳答案

I would like to output the data so it is sorted according to the element in an alphabetical order. So all of the 'Democratic' presidents would appear first.

然后,让我们用一个示例进行测试,其中元素的自然顺序不会表明民主党总统在输出中排在第一位:

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>

然后,开始对数据进行排序。目前,您在匹配 president 的模板上下文中应用 xsl:sort,如下所示:

<xsl:apply-templates select="party">
<xsl:sort select="party"/>
</xsl:apply-templates>

但是你要排序的单位不是party,而是president元素。只是 conditionparty 元素的文本内容,它是 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>

关于xml - 使用 <xsl :apply-templates> and <xsl:sort>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26368839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com