Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic XSLT to tabluate XML

Tags:

html

xml

xslt

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 ?

like image 605
user2571142 Avatar asked Jul 12 '13 00:07

user2571142


2 Answers

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.

like image 108
Michael Kay Avatar answered Sep 27 '22 17:09

Michael Kay


I've added a bit of CSS to the excellent solution from Michael Kay, if anyone is looking for a quick way to make some XML readable in a browser:

<?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="/">
    <style>
      body {font-family: sans-serif;}
      td {padding: 4px;}
    </style>
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="*">
    <tr>
      <td style="background-color: #aaa;">
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td style="background-color: #ccc;">
        <p><xsl:value-of select="."/></p>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="*[*]">
    <tr>
      <td style="border:2px solid #c55; font-size:120%;">
        <p><xsl:value-of select="name()"/></p>
      </td>
      <td style="">
        <table>
          <xsl:apply-templates/>
        </table>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
like image 22
flash Avatar answered Sep 27 '22 16:09

flash