Self-repeating increasing autonumber in XSL



DISCLAIMER


I recently asked a question about complex autonumbers in XSL and quickely got a very good answer. It was only later that I realised I had forgotten one detail from my example. I'm not sure what is Stackoverflow's recommended policy with this: should I make a new question or start to modify my earlier post? As I already got a good answer for what I asked and accepted that, I decided it is better to make a new question as it is a bit of a different thing from what I asked earlier.


Question


So this is the XML:



<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<XML>
<thing>line</thing>
<thing>line</thing>
<thing>line</thing>
<thing>line</thing>
</XML>


I need to transform that into new XML which has the following structure at the beginning:



<ids>
<id id="1" nr="0"/>
<id id="2" nr="10"/>
<id id="3" nr="10"/>
<id id="4" nr="20"/>
<id id="5" nr="20"/>
<id id="6" nr="30"/>
<id id="7" nr="30"/>
<id id="8" nr="40"/>
</ids>


The following stylesheet that I got as a reply to my earlier question almost does this.



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR"
xmlns:xs="http://ift.tt/tphNwY"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" omit-xml-declaration="no" />
<xsl:template match="/">
<XML>
<ids>
<xsl:for-each select="1 to xs:integer(count(//thing) * 2)">
<id id="{position()}" nr="{(position() - 1)*10}"/>
</xsl:for-each>
</ids>
</XML>
</xsl:template>
</xsl:stylesheet>


It produces this:



<?xml version="1.0" encoding="UTF-8"?>
<XML>
<ids>
<id id="1" nr="0"/>
<id id="2" nr="10"/>
<id id="3" nr="20"/>
<id id="4" nr="30"/>
<id id="5" nr="40"/>
<id id="6" nr="50"/>
<id id="7" nr="60"/>
<id id="8" nr="70"/>
</ids>
</XML>


However, I would like to have in the nr's the pattern 0, 10, 10, 20, 20, 30, 30, 40, etc. for as many times as is needed to have the double amount of numbers than there were thing-nodes in the original XML. I've been trying to figure it out over the last few days, but in the end had to ask again. In all other aspects this XSL works now really well also with my real data.


No comments:

Post a Comment