我想知道是否有可能得到一个范围内的数字,并将它们打印成自己的元素。
假设我有一个输入,其中有很多元素包含一个数字,其中有些包含一个数字范围:
<root>
<ele>
<no>1</no>
</ele>
<ele>
<no>3</no>
</ele>
<ele>
<no>4-11</no>
</ele>
<ele>
<no>12</no>
</ele>
我想得到这个(缩短到11,就像在输入中):
<root>
<ele>
<no>1</no>
</ele>
<ele>
<no>3</no>
</ele>
<ele>
<no>4</no>
</ele>
<ele>
<no>5</no>
</ele>
<ele>
<no>6</no>
</ele>
到目前为止,我已经提出了一个XSLT:
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//no">
<no>
<xsl:if test="not(contains(.,'-'))"><xsl:value-of select="."/></xsl:if>
<xsl:if test="contains(.,'-')">
<xsl:variable name="beforehiven">
<xsl:value-of select="substring-before(.,'-')"/>
</xsl:variable>
<xsl:variable name="afterhiven">
<xsl:value-of select="substring-after(.,'-')"/>
</xsl:variable>
<xsl:variable name="diff">
<xsl:value-of select="$afterhiven - $beforehiven"/>
</xsl:variable>
<xsl:value-of select="$diff"/>
</xsl:if>
</no>
</xsl:for-each>
</xsl:template>首先,我把那些没有蜂箱的放进去,然后输出它们。我知道介于4到11之间是6个数字,所以我必须创建新的<ele>和<no>元素,并给它们值7-1,为下一个变量创建一个新变量6-1等等。
XSLT有可能做到这一点吗?如果是,怎么做?
耽误您时间,实在对不起!
编辑:我正在使用XSLT2.0版本
完整的产出应是:
<root>
<ele>
<no>1</no>
</ele>
<ele>
<no>3</no>
</ele>
<ele>
<no>4</no>
</ele>
<ele>
<no>5</no>
</ele>
<ele>
<no>6</no>
</ele>
<ele>
<no>7</no>
</ele>
<ele>
<no>8</no>
</ele>
<ele>
<no>9</no>
</ele>
<ele>
<no>10</no>
</ele>
<ele>
<no>11</no>
</ele>
<ele>
<no>12</no>
</ele>
发布于 2015-05-04 07:50:40
对于XSLT2.0,这应该非常简单--尝试:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ele[contains(no, '-')]">
<xsl:variable name="from" select="substring-before(no, '-')" />
<xsl:variable name="to" select="substring-after(no, '-')"/>
<xsl:for-each select="xs:integer($from) to xs:integer($to)">
<ele>
<no>
<xsl:value-of select="."/>
</no>
</ele>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/30024616
复制相似问题