在这种情况下,我需要检查属性值,这些属性值可能会被依次编号,并在开始值和结束值之间输入一个破折号。
<root>
<ref id="value00008 value00009 value00010 value00011 value00020"/>
</root>
理想的输出是..。
8-11, 20
我可以将属性标记为单独的值,但我不确定如何检查"valueXXXXX“末尾的数字是否连续于前一个值。
我正在使用XSLT2.0
发布于 2013-10-19 01:40:17
您可以在xsl:for-each-group
和@group-adjacent
测试中使用number()
值减去position()
。
这个把戏显然是由戴维·卡莱尔,据迈克尔·凯说。发明的。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vals"
select="tokenize(root/ref/@id, '\s?value0*')[normalize-space()]"/>
<xsl:variable name="condensed-values" as="item()*">
<xsl:for-each-group select="$vals"
group-adjacent="number(.) - position()">
<xsl:choose>
<xsl:when test="count(current-group()) > 1">
<!--a sequence of successive numbers,
grab the first and last one and join with '-' -->
<xsl:sequence select="
string-join(current-group()[position()=1
or position()=last()]
,'-')"/>
</xsl:when>
<xsl:otherwise>
<!--single value group-->
<xsl:sequence select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:variable>
<xsl:value-of select="string-join($condensed-values, ',')"/>
</xsl:template>
</xsl:stylesheet>
https://stackoverflow.com/questions/19459806
复制相似问题