gpt4 book ai didi

sorting - XSLT 数字节点不排序

转载 作者:行者123 更新时间:2023-12-04 18:23:25 24 4
gpt4 key购买 nike

我有以下xml:

<policy>
<games>
<game startTime="11:00"/>
<game startTime="11:20"/>
<game startTime="11:40"/>
</games>
<games>
<game startTime="11:10"/>
<game startTime="11:30"/>
<game startTime="11:50"/>
</games>
</Policy>

我正在尝试编写一个 xslt,它将为每个游戏节点添加一个新属性并按时间顺序添加值,例如
<policy>
<games>
<game startTime="11:00" id="1"/>
<game startTime="11:20" id="3"/>
<game startTime="11:40" id="5"/>
</games>
<games>
<game startTime="11:10" id="2"/>
<game startTime="11:30" id="4"/>
<game startTime="11:50" id="6"/>
</games>
</policy>

我需要游戏节点保持其当前顺序,所以我不确定 xsl:sort 是否有效。

目前我有这个显然只是按照他们当前的顺序给他们编号,不会考虑时间属性:
<xsl:template match="game">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="count(preceding::game) + 1"/>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

最佳答案

我希望有比这更好的方法:

<xsl:template match="game">
<xsl:copy>
<xsl:variable name="time" select="@startTime" />
<xsl:for-each select="//game">
<xsl:sort select="@startTime" />
<xsl:if test="current()/@startTime = $time">
<xsl:attribute name="id">
<xsl:value-of select="position()"/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

关于sorting - XSLT 数字节点不排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19816760/

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