How to use XSLT to convert a XML to Table [CODE UPDATED 11/6] -
i have xml file want convert table or csv xslt. tried altova mapforce 2014to map columns still,not able gen out outcomes. lot.
xml file have
<records> <person id="756252" date="15-oct-2014"> <gender>male</gender> <namedetails> <name nametype="primary name"> <namevalue> <firstname>ken</firstname> <surname>wu</surname> </namevalue> </name> <name nametype="aka"> <namevalue> <firstname>kenneth</firstname> <surname>wu</surname> </namevalue> </name> <name nametype="aka2"> <namevalue> <firstname>can</firstname> <surname>wu</surname> </namevalue> </name> </namedetails> <descriptions> <description description1="11" description2="12" description3="13"/> <description description1="21" description2="22" description3="23"/> <description description1="31" description2="32" description3="33"/> </descriptions> <datedetails> <date datetype="date of birth"> <datevalue year="1990" /> <datevalue year="1991" /> </date> <date datetype="date of issue"> <datevalue year="2000" /> <datevalue year="2001" /> </date> </datedetails> </person> <person id="253555" date="14-oct-2014"> <gender>male</gender> <namedetails> <name nametype="primary name"> <namevalue> <firstname>peter</firstname> <surname>lai</surname> </namevalue> </name> </namedetails> <descriptions> <description description1="11" description2="12" description3="13"/> <description description1="21" description2="22"/> </descriptions> <date datetype="date of birth"> <datevalue year="1992" /> </date> </person> </records>
expected outcome
|id |date |gender |nametype |firstname |surname |description1 |description2 |description3 |datetype |year| |756252 |15-oct-2014 |male |primary name |ken |wu |11 |12 |13 |date of birth |1990| |756252 |15-oct-2014 |male |primary name |ken |wu |21 |22 |23 |date of birth |1990| |756252 |15-oct-2014 |male |primary name |ken |wu |31 |32 |33 |date of birth |1990| |756252 |15-oct-2014 |male |aka |kenneth |wu |11 |12 |13 |date of birth |1990| |756252 |15-oct-2014 |male |aka |kenneth |wu |21 |22 |23 |date of birth |1990| |756252 |15-oct-2014 |male |aka |kenneth |wu |31 |32 |33 |date of birth |1990| |756252 |15-oct-2014 |male |aka2 |can |wu |11 |12 |13 |date of birth |1990| |756252 |15-oct-2014 |male |aka2 |can |wu |21 |22 |23 |date of birth |1990| |756252 |15-oct-2014 |male |aka2 |can |wu |31 |32 |33 |date of birth |1990| |756252 |15-oct-2014 |male |primary name |ken |wu |11 |12 |13 |date of issue |1991| |756252 |15-oct-2014 |male |primary name |ken |wu |21 |22 |23 |date of issue |1991| |756252 |15-oct-2014 |male |primary name |ken |wu |31 |32 |33 |date of issue |1991| |756252 |15-oct-2014 |male |aka |kenneth |wu |11 |12 |13 |date of issue |1991| |756252 |15-oct-2014 |male |aka |kenneth |wu |21 |22 |23 |date of issue |1991| |756252 |15-oct-2014 |male |aka |kenneth |wu |31 |32 |33 |date of issue |1991| |756252 |15-oct-2014 |male |aka2 |can |wu |11 |12 |13 |date of issue |1991| |756252 |15-oct-2014 |male |aka2 |can |wu |21 |22 |23 |date of issue |1991| |756252 |15-oct-2014 |male |aka2 |can |wu |31 |32 |33 |date of issue |1991| |756252 |15-oct-2014 |male |primary name |ken |wu |11 |12 |13 |date of issue |2000| |756252 |15-oct-2014 |male |primary name |ken |wu |21 |22 |23 |date of issue |2000| |756252 |15-oct-2014 |male |primary name |ken |wu |31 |32 |33 |date of issue |2000| |756252 |15-oct-2014 |male |aka |kenneth |wu |11 |12 |13 |date of issue |2000| |756252 |15-oct-2014 |male |aka |kenneth |wu |21 |22 |23 |date of issue |2000| |756252 |15-oct-2014 |male |aka |kenneth |wu |31 |32 |33 |date of issue |2000| |756252 |15-oct-2014 |male |aka2 |can |wu |11 |12 |13 |date of issue |2000| |756252 |15-oct-2014 |male |aka2 |can |wu |21 |22 |23 |date of issue |2000| |756252 |15-oct-2014 |male |aka2 |can |wu |31 |32 |33 |date of issue |2000| |756252 |15-oct-2014 |male |primary name |ken |wu |11 |12 |13 |date of issue |2001| |756252 |15-oct-2014 |male |primary name |ken |wu |21 |22 |23 |date of issue |2001| |756252 |15-oct-2014 |male |primary name |ken |wu |31 |32 |33 |date of issue |2001| |756252 |15-oct-2014 |male |aka |kenneth |wu |11 |12 |13 |date of issue |2001| |756252 |15-oct-2014 |male |aka |kenneth |wu |21 |22 |23 |date of issue |2001| |756252 |15-oct-2014 |male |aka |kenneth |wu |31 |32 |33 |date of issue |2001| |756252 |15-oct-2014 |male |aka2 |can |wu |11 |12 |13 |date of issue |2001| |756252 |15-oct-2014 |male |aka2 |can |wu |21 |22 |23 |date of issue |2001| |756252 |15-oct-2014 |male |aka2 |can |wu |31 |32 |33 |date of issue |2001| |253555 |14-oct-2014 |male |primary name |peter |lai |11 |12 |13 |date of issue |1992| |253555 |14-oct-2014 |male |primary name |peter |lai |21 |22 | |date of issue |1992|
xsl create testing
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <head> <title>records</title> </head> <body> <table border="1"> <tr> <th>id</th> <th>date</th> <th>gender</th> <th>nametype</th> <th>firstname</th> <th>surname</th> <th>description1</th> <th>description2</th> <th>description3</th> </tr> <xsl:for-each select="records/person/namedetails/name"> <tr> <td><xsl:value-of select="../../@id"/></td> <td><xsl:value-of select="../../@date"/></td> <td><xsl:value-of select="../../gender"/></td> <td><xsl:value-of select="@nametype"/></td> <td><xsl:value-of select="namevalue/firstname"/></td> <td><xsl:value-of select="namevalue/surname"/></td> <td><xsl:value-of select="../../descriptions/description/@description1"/></td> <td><xsl:value-of select="../../descriptions/description/@description2"/></td> <td><xsl:value-of select="../../descriptions/description/@description3"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
you need 2 nested loops here:
... <xsl:for-each select="records/person/namedetails/name"> <xsl:variable name="nametype" select="@nametype"/> <xsl:variable name="firstname" select="namevalue/firstname"/> <xsl:variable name="surname" select="namevalue/surname"/> <xsl:for-each select="../../descriptions/description"> <tr> <td><xsl:value-of select="../../@id"/></td> <td><xsl:value-of select="../../@date"/></td> <td><xsl:value-of select="../../gender"/></td> <td><xsl:value-of select="$nametype"/></td> <td><xsl:value-of select="$firstname"/></td> <td><xsl:value-of select="$surname"/></td> <td><xsl:value-of select="@description1"/></td> <td><xsl:value-of select="@description2"/></td> <td><xsl:value-of select="@description3"/></td> </tr> </xsl:for-each> </xsl:for-each> ...
edit:
to avoid storing name details in individual variables, can do:
<xsl:for-each select="records/person/namedetails/name"> <xsl:variable name="name" select="."/> <xsl:for-each select="../../descriptions/description"> <tr> <td><xsl:value-of select="../../@id"/></td> <td><xsl:value-of select="../../@date"/></td> <td><xsl:value-of select="../../gender"/></td> <td><xsl:value-of select="$name/@nametype"/></td> <td><xsl:value-of select="$name/namevalue/firstname"/></td> <td><xsl:value-of select="$name/namevalue/surname"/></td> <td><xsl:value-of select="@description1"/></td> <td><xsl:value-of select="@description2"/></td> <td><xsl:value-of select="@description3"/></td> </tr> </xsl:for-each> </xsl:for-each>
edit 2:
here's example of 3 nested loops, adding dates have added in latest edit.
<xsl:for-each select="records/person/namedetails/name"> <xsl:variable name="name" select="."/> <xsl:for-each select="../../datedetails/date/datevalue"> <xsl:variable name="dateval" select="."/> <xsl:for-each select="../../../descriptions/description"> <tr> <td><xsl:value-of select="../../@id"/></td> <td><xsl:value-of select="../../@date"/></td> <td><xsl:value-of select="../../gender"/></td> <td><xsl:value-of select="$name/@nametype"/></td> <td><xsl:value-of select="$name/namevalue/firstname"/></td> <td><xsl:value-of select="$name/namevalue/surname"/></td> <td><xsl:value-of select="@description1"/></td> <td><xsl:value-of select="@description2"/></td> <td><xsl:value-of select="@description3"/></td> <td><xsl:value-of select="$dateval/../@datetype"/></td> <td><xsl:value-of select="$dateval/@year"/></td> </tr> </xsl:for-each> </xsl:for-each> </xsl:for-each>
Comments
Post a Comment