Reputation: 49
I wonder if it is possible to get the numbers within a range and print them into own elements.
Say I have an input with a lot of elements with a number in it, some of them containing a numberrange:
<root>
<ele>
<no>1</no>
</ele>
<ele>
<no>3</no>
</ele>
<ele>
<no>4-11</no>
</ele>
<ele>
<no>12</no>
</ele>
and I want to get this (shortened, up to 11, like in the input):
<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>
So far I have come up with this 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>
First I adress the ones without a hiven and output them. And I know that between 4 and 11 are 6 numbers, so I have to create new <ele>
and <no>
elements and give them the value 7-1, create a new variable 6-1 for the next one and so on.
Is this possible with XSLT? If yes, how?
Thanks for your time!
edit: I am using xslt version 2.0
the complete output should be:
<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>
Upvotes: 0
Views: 39
Reputation: 117073
This should be quite simple with XSLT 2.0 Try:
<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>
Upvotes: 1