Vertical column data filling using XSLT
Im new to XSLT and My XML is below
<Aus> <au> <ele> <auid>Au1</auid> <cid>C1</cid> <fn>F1</fn> <sn>S1</sn> <dept>D1</dept> </ele> <ele> <auid>Au2</auid> <cid>C2</cid> <fn>F2</fn> <sn>S2</sn> <dept>D2</dept> </ele> <ele> <auid>Au3</auid> <cid>C3</cid> <fn>F3</fn> <sn>S3</sn> <dept>D4</dept> </ele>.............. </au> </Aus>
I want the output like below in html view using XSLT conversion
but XSLT code should be simple to identify next columns by position increment. Please help me.
My current code is
<xsl:for-each select="//Aus/au"> <table> <tr> <td><xsl:value-of select="ele[1]/auid"/></td><td><xsl:value-of select="ele[2]/auid"/></td><td><xsl:value-of select="ele[3]/auid"/></td> </tr> <tr> <td><xsl:value-of select="ele[1]/cid"/></td><td><xsl:value-of select="ele[2]/cid"/></td><td><xsl:value-of select="ele[3]/cid"/></td> </tr> .......... </table> </xsl:for-each>
I would do it like this:
<xsl:template match="Aus/au"> <table> <tbody> <xsl:apply-templates select="ele[1]/*" mode="row"/> </tbody> </table> </xsl:template> <xsl:template match="ele/*" mode="row"> <tr> <xsl:variable name="pos" select="position()"/> <xsl:apply-templates select="../../ele/*[$pos]"/> </tr> </xsl:template> <xsl:template match="ele/*"> <td> <xsl:value-of select="."/> </td> </xsl:template>
https://xsltfiddle.liberty-development.net/gVhEaiK
The sample you linked in your comment seems to have more complex input data as it seems there are nested elements, also there seem to be lots of elements without data; however, the templates could be adapted to e.g.
<xsl:template match="authorDetails/authors"> <table> <tbody> <xsl:apply-templates select="element[1]/descendant::*[not(*)]" mode="row"/> </tbody> </table> </xsl:template> <xsl:template match="element//*" mode="row"> <tr> <th> <xsl:value-of select="local-name()"/> </th> <xsl:variable name="pos" select="position()"/> <xsl:apply-templates select="ancestor::authors/element/descendant::*[not(*)][$pos]"/> </tr> </xsl:template> <xsl:template match="element//*"> <td> <xsl:value-of select="."/> </td> </xsl:template>
Example: https://xsltfiddle.liberty-development.net/gVhEaiK/5