Monday 7 May 2012

How to put the node name into the result?


INPUT:-
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <G>
    <R>
      <SUB1>
        <AA>blahAA</AA>
        <AB>blahAB</AB>
        <AC>blahAC</AC>
      </SUB1>
      <SUB2>
        <BA>blahBA</BA>
        <BB>blahBB</BB>
      </SUB2>
      <SUB3>
        <CA>blahCA</CA>
        <CB>blahCB</CB>
        <CC>blahCC</CC>
        <CD>blahCD</CD>
      </SUB3>
    </R>
  </G>
</root>


Expected Output:-
GROUPA GROUPB SUBGROUP NAME VALUE
G      R      SUB1     AA   blahAA
G      R      SUB1     AB   blahAB
G      R      SUB1     AC   blahAC
G      R      SUB2     BA   blahBA
G      R      SUB2     BB   blahBB
G      R      SUB3     CA   blahCA
G      R      SUB3     CB   blahCB
G      R      SUB3     CC   blahCC
G      R      SUB3     CD   blahCD


XSLT:-
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0" xmlns:vish="vishnu.com">
  <xsl:template match="*[*]">
    <xsl:param name="tablerow" select="''"/>
    <xsl:apply-templates select="*">
      <xsl:with-param name="tablerow">
        <xsl:copy-of select="$tablerow"/>
        <td>
          <xsl:value-of select="local-name(.)"/>
        </td>
      </xsl:with-param>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="*[not (*)]">
    <xsl:param name="tablerow" select="''"/>
    <tr>
      <xsl:copy-of select="$tablerow"/>
      <td>
        <xsl:value-of select="local-name(.)"/>
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="/*">
    <html>
      <body>
        <table>
          <tr>
            <th>GROUPA</th>
            <th>GROUPB</th>
            <th>SUBGROUP</th>
            <th>NAME</th>
            <th>VALUE</th>
          </tr>
          <xsl:apply-templates select="*"/>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

No comments: