循环不同的值 [英] Looping over distinct values

查看:23
本文介绍了循环不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<State>AL</State><State>AL</State><State>NM</State></状态>

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

<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 属性更改的内容:

输入

XSLT

输出

Given a variable which returns a list of distinct States using the distinct-values() function, is there a way to tokenize the variable in a for-each loop?

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

The following variable returns AL and NM, but I can't iterate over it using for-each. Is there a way around this?

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

XSLT 2.0 ok.

解决方案

The distinct-values() function returns a sequence of values which you should be able to iterate over. The result is so to speak "tokenized".

fn:distinct-values('AL', 'AL', 'NL') returns the sequence ('AL', 'NL').

If you output the variable with xsl:value-of it will return the string "AL NL" only because the default sequence separator for xsl:value-of is a single space character. This is something you could change with the @separator attribute:

Input

<?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>

Output

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

这篇关于循环不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆