给定一个使用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好的。
发布于 2010-07-15 19:52:25
fn:distinct-values('AL', 'AL', 'NL')返回序列('AL', 'NL')。
这是您可以使用@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:发布于 2013-09-19 12:33:51
下面是我过去使用过的XSLT1.0解决方案。
<xsl:template match="/">
<ul>
<xsl:for-each select="//State[not(.=preceding::*)]">
<li>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</xsl:template>返回:
<ul xmlns="http://www.w3.org/1999/xhtml">
<li>AL</li>
<li>NM</li>
</ul>发布于 2010-07-15 19:21:22
理论上它应该可以工作;您确定给distinct-values函数的XPath是正确的吗?您给出的代码要求States元素是forms元素的同级元素。
您可以紧跟在变量声明之后插入<xsl:value-of select="count($FormStates)">,以确认是否正确设置了该变量。
https://stackoverflow.com/questions/3246216
复制相似问题