Generic XSLT to tabluate XML

html, xml, xslt

Solution

Here's a refinement of the solution from @ABach, which attempts to create nested tables:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="*">
    <tr>
      <td>
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td>
        <p><xsl:value-of select="."/></p>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="*[*]">
    <tr>
      <td>
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td>
        <table>
          <xsl:apply-templates/>
        </table>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

I haven't attempted to do anything very clever with mixed content.

Problem

I am working to create an XSLT which is generic enough to create a table of name-value of any input XML data. Eg. ``` <root> <Field1>value1</Field1> <Field2>value2</Field2> <Field3>value3</Field3> </root> ``` Output should look like : ``` <table> <tr> <td>Field1</td> <td>value1</td> </tr> <tr> <td>Field2</td> <td>value2</td> </tr> <tr> <td>Field3</td> <td>value3</td> </tr> </table> ``` I want to avoid using xml tag names in XSLT code, so as to make it generic enough. Not sure if this is possible at all . Any ideas how to go about this ?

Original source