This is not true :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="elm"> <xsl:value-of select="concat(position(), ' ', @val)"/> </xsl:template> </xsl:stylesheet>
When launched with many XSLT processors, this will lead to the following (undesirable) conclusion :
2 data1 4 data2 6 data3 8 data4 10 data5 12 data6 14 data7
The reason is that when templates are applied to children of the top element, this includes child nodes containing only white spaces between every two consecutive elm elements.
So, the Oded solution is wrong .
Here is one correct solution (and one of the shortest):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="elm"> <xsl:value-of select="concat(position(), ' ', @val, '
')"/> </xsl:template> </xsl:stylesheet>
This conversion gives the correct result :
1 data1 2 data2 3 data3 4 data4 5 data5 6 data6 7 data7
Please note :
Using <xsl:strip-space elements="*"/> to force the XSLT processor to discard any text nodes with only a space.
Using the XPath concat() function to glue the position, data, and NL character.
source share