XML : XSLT to CSV: output element to multiple rows

I appreciate if anyone could help me with this. I am trying to output this XML into a CSV file using the corresponding XSLT transformation. The user has many roles and groups assigned to him/her. Some roles are assigned directly, and some other through groups. I want new rows for the user based on each role or group assigned to her like this:

  User;Account;Service;Group;Role  Natalie Petit; petna; S1; G1; R1;   Natalie Petit; petna; S1; G1; R2;   Natalie Petit; petna;S2;;R1;   Natalie Petit; petna;S2;;R3;     

This is the XML source:

   <?xml version="1.0" encoding="UTF-8"?>  <provider>  <service>  <name>S1</name>  <group>  <name>G1</name>  <role>  <name>R1</name>  <name>R2</name>  </role>  </group>  </service>    <service>  <name>S2</name>  <group/>  <role>  <name>R1</name>  <name>R3</name>  </role>  </service>  <user>  <name>Natalie Petit</name>  <account>petna</account>  </user>  </provider>    

And this my XSLT, but doesn't get me there:

  <?xml version="1.0" encoding="UTF-8"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="@*|node()">  <xsl:text>User;Account;Service;Group;Role</xsl:text>    <xsl:apply-templates/>    </xsl:template>    <xsl:template match="service">  <xsl:for-each select=".">      <xsl:value-of select="name"/>      <xsl:text>;</xsl:text>      </xsl:for-each>      <xsl:apply-templates select="role"/>        <xsl:apply-templates select="group"/>  </xsl:template>    <xsl:template match="group">  <xsl:for-each select=".">      <xsl:value-of select="name"/>      <xsl:text>;</xsl:text>      </xsl:for-each>  <xsl:apply-templates select="role"/>  </xsl:template>    <xsl:template match="role">  <xsl:for-each select=".">      <xsl:value-of select="name"/>      <xsl:text>;</xsl:text>      </xsl:for-each>  </xsl:template>    

No comments:

Post a Comment