gpt4 book ai didi

html - XSLT 合并具有相同值的行

转载 作者:行者123 更新时间:2023-12-03 17:10:18 25 4
gpt4 key购买 nike

我对 XML 和 XSLT 文档还很陌生。我创建了一个 XML 文档,其中包含跨越几个月的天气数据记录。我想使用 XSLT 文档将每个表格行中同一月份的记录组合在一起,然后在跨越同一月份所有行的列上打印它们的月份(如果月份 = 2,则将打印二月)。有什么方法可以使用 if-else 语句打印出相应的月份,如果表格行具有相同的月份值,我该如何合并它们?
XML:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="S3(1).xsl" ?>

<forecast xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="S3.xsd"
qTime="28/10/20 10:00 PM">

<weather yyyymmdd="20200430">
<year>2020</year>
<month>04</month>
<date>30</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.6</highest>
<lowest>28.4</lowest>
</weather>
<weather yyyymmdd="20200218">
<year>2020</year>
<month>02</month>
<date>18</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>34.6</highest>
<lowest>30.5</lowest>
</weather>
<weather yyyymmdd="20200710">
<year>2020</year>
<month>07</month>
<date>10</date>
<comment>Partly sunny</comment>
<code>partlySunny</code>
<highest>33.1</highest>
<lowest>29.2</lowest>
</weather>
<weather yyyymmdd="20200616">
<year>2020</year>
<month>06</month>
<date>16</date>
<comment>Considerable clouds</comment>
<code>cloudy</code>
<highest>30.5</highest>
<lowest>25.4</lowest>
</weather>
<weather yyyymmdd="20200612">
<year>2020</year>
<month>06</month>
<date>12</date>
<comment>Cloudy with a thunderstorm</comment>
<code>thunderstorm</code>
<highest>29.1</highest>
<lowest>23.2</lowest>
</weather>
<weather yyyymmdd="20200421">
<year>2020</year>
<month>04</month>
<date>21</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.2</highest>
<lowest>29.8</lowest>
</weather>
<weather yyyymmdd="20200628">
<year>2020</year>
<month>06</month>
<date>28</date>
<comment>A morning shower, then rain</comment>
<code>rain</code>
<highest>30.2</highest>
<lowest>22.7</lowest>
</weather>
<weather yyyymmdd="20200502">
<year>2020</year>
<month>05</month>
<date>02</date>
<comment>Cloudy with a thunderstorm</comment>
<code>thunderstorm</code>
<highest>28.1</highest>
<lowest>26.9</lowest>
</weather>
<weather yyyymmdd="20200428">
<year>2020</year>
<month>04</month>
<date>28</date>
<comment>A morning shower</comment>
<code>rain</code>
<highest>28.8</highest>
<lowest>22.2</lowest>
</weather>
<weather yyyymmdd="20200410">
<year>2020</year>
<month>04</month>
<date>10</date>
<comment>Partly sunny</comment>
<code>partlySunny</code>
<highest>33.7</highest>
<lowest>29.3</lowest>
</weather>
<weather yyyymmdd="20200730">
<year>2020</year>
<month>07</month>
<date>30</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>32.3</highest>
<lowest>28.4</lowest>
</weather>
<weather yyyymmdd="20200706">
<year>2020</year>
<month>07</month>
<date>06</date>
<comment>Plenty of sunshine</comment>
<code>sunny</code>
<highest>34.5</highest>
<lowest>30.6</lowest>
</weather>
</forecast>
到目前为止,这是我的 XSL 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="forecast">
<h1>
<span> <xsl:value-of select="@qLocation"/></span>
<span>[<xsl:value-of select="@qTime"/>]</span>
</h1>

<table style="border:1px solid black;">
<tr>
<th>Date</th>
<th>Weather data</th>
<th>Highest</th>
<th>Lowest</th>
</tr>

<xsl:for-each select="weather">
<xsl:sort select="month" order="ascending"/>
<xsl:sort select="date" order="ascending"/>
<tr>
<td>
<span><xsl:value-of select="date"/>/</span>
<span><xsl:value-of select="month"/></span>
</td>
<td>
<li>
<xsl:value-of select="date"/>/
<xsl:value-of select="month"/>/
<xsl:value-of select="year"/>,
from
<xsl:value-of select="lowest"/>C
to
<xsl:value-of select="highest"/>C,
<xsl:value-of select="comment"/>

</li>
</td>
<td>
<xsl:value-of select="highest"/>C
<!-- <img src="img1.jpg"> -->
</td>
<td>
<xsl:value-of select="lowest"/>C
<!-- <img src="img2.jpg"> -->
</td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我试图实现的输出是这样的:
Image

最佳答案

为此,我会使用 xsl:for-each-group 指定 month grouping-key 的值:<xsl:for-each-group select="weather" group-by="month">在循环内部,迭代 current-group()处理每个分组的 weather生成列表的元素: <xsl:for-each select="current-group()">您可以获得min()max()来自 weather 分组集中的值元素:max(current-group()/highest)使用月份编号作为位置,您可以使用谓词从月份缩写序列中进行选择。有一个前导零,所以使用 number()转换,即 022 :<xsl:sequence select="('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[number(current-grouping-key())]"/>

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="forecast">
<h1>
<span> <xsl:value-of select="@qLocation"/></span>
<span>[<xsl:value-of select="@qTime"/>]</span>
</h1>

<table style="border:1px solid black;">
<tr>
<th>Date</th>
<th>Weather data</th>
<th>Highest</th>
<th>Lowest</th>
</tr>

<xsl:for-each-group select="weather" group-by="month">
<xsl:sort select="month" order="ascending"/>
<xsl:sort select="date" order="ascending"/>
<tr>
<td>
<span><xsl:value-of select="year"/>/</span>
<span><xsl:sequence select="('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')[number(current-grouping-key())]"/></span>
</td>
<td>
<xsl:for-each select="current-group()">
<li>
<xsl:value-of select="date"/>/
<xsl:value-of select="month"/>/
<xsl:value-of select="year"/>,
from
<xsl:value-of select="lowest"/>C
to
<xsl:value-of select="highest"/>C,
<xsl:value-of select="comment"/>

</li>
</xsl:for-each>
</td>
<td>
<xsl:value-of select="max(current-group()/highest)"/>C
<!-- <img src="img1.jpg"> -->
</td>
<td>
<xsl:value-of select="min(current-group()/lowest)"/>C
<!-- <img src="img2.jpg"> -->
</td>
</tr>
</xsl:for-each-group>
</table>
</xsl:template>

</xsl:stylesheet>

关于html - XSLT 合并具有相同值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64632124/

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