gpt4 book ai didi

xslt - 循环不同的值

转载 作者:行者123 更新时间:2023-12-02 11:41:48 25 4
gpt4 key购买 nike

给定一个使用distinct-values()函数返回不同状态列表的变量,有没有办法在for-each循环中标记该变量?

<States>
<State>AL</State>
<State>AL</State>
<State>NM</State>
</States>

以下变量返回 AL 和 NM,但我无法使用 for-each 对其进行迭代。有办法解决这个问题吗?

<xsl:variable name="FormStates" select="distinct-values(States/State)"/>
<xsl:for-each select="$FormStates">

XSLT 2.0 好。

最佳答案

distinct-values()函数返回一个您应该能够迭代的值序列。结果可以说是“代币化”。

fn:distinct-values('AL', 'AL', 'NL')返回序列 ('AL', 'NL') .

如果输出变量为 xsl:value-of它只会返回字符串“AL NL”,因为 xsl:value-of 的默认序列分隔符是单个空格字符。您可以使用 @separator 更改此内容属性:

输入

<?xml version="1.0" encoding="UTF-8"?>
<States>
<State>AL</State>
<State>AL</State>
<State>NM</State>
</States>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:variable name="FormStates" select="distinct-values(States/State)"/>
<xsl:comment>xsl:value-of</xsl:comment>
<xsl:value-of select="$FormStates" separator=":"/>
<xsl:comment>xsl:for-each</xsl:comment>
<xsl:for-each select="$FormStates">
<xsl:value-of select="."/>
<xsl:text>:</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

输出

<?xml version="1.0" encoding="UTF-8"?>
<!--xsl:value-of-->
AL:NM
<!--xsl:for-each-->
AL:NM:

关于xslt - 循环不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3246216/

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