I'am newbie in XSL i have an xml file like above and i would like to transform it to csv i use java main to execute it but i has a problem with geting position number in xsl file
<parent>
<child name="a" type="1"/>
<child name="b" type="2"/>
<child name="c" type="1"/>
<child name="d" type="3"/>
</parent>
The output is :
a 1
b 2
c 1
d 3
but what i would like to get is :
child name type
1 a 1
2 b 2
3 c 1
4 d 3
with the first column should be the child position
this is my xsl file
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="child">
<!--header row-->
<xsl:for-each select="child">
<xsl:number value="position()" format="1" />
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:for-each select="@*">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:value-of select="';'"/>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
So to summarize i have two problem how can add header row like the example and how can get position of child and add it
You better match the parent, print the header row, then loop over the children:
You can use
concat()
as below:As noted in other answers, your main problem is context: you must be in the context of
parent
in order to do<xsl:for-each select="child">
.If you want this to be dynamic, including the header row, try: